The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf
file (from your App_Data
directory to the output directory - typically .\bin\debug
- where you app runs) and most likely, your INSERT
works just fine - but you're just looking at the wrong .mdf file in the end!
The REAL solution in my opinion would be to
install SQL Server Management Studio Express (if you haven't already)
create your database in SSMS Express, give it a logical name (e.g.
MyDatabase
)connect to it using its logical database name (given when you create it on the server), and let the server handle all the file-related details; don't mess around with physical files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=MyDatabase;Integrated Security=True
and everything else is exactly the same as before...