All data from database is deleted when closing the application

风流意气都作罢 提交于 2019-11-28 10:08:22

问题


What is the proper way to create a local-database file and then connect the app to it ? I want it to work even if you change the location of the project folder.

Right know what I do is: Project -> Add new item -> Service-based Database and I create one and then I go to Data -> Add new Data Source, I add the created database and I get the connection string.

Ok, all good, I can connect to it as I wish BUT all my data is erased from the database when I close the application (not always).

For example, this code:

SqlConnection c = new SqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB.mdf;Integrated Security=True;User Instance=True");           
c.Open();
SqlCommand cmd;
cmd = new SqlCommand("CREATE TABLE Persons (id int primary key, nume char(20), age int)");
cmd.ExecuteNonQuery();
cmd = new SqlCommand("INSERT INTO Persons VALUES (@id, @name, @age)", c);
cmd.Parameters.AddWithValue("@id", 1);
cmd.Parameters.AddWithValue("@name", "Catalin");
cmd.Parameters.AddWithValue("@age", 20);
cmd.ExecuteNonQuery();

I run it first time to create the table and add an item to it and then, then if I run it the second time without the sqlcommand to create the table persons, it tells me that there is no Persons object, BUT if I run the second time the project with the same code, it tells me that there is already a Persons object...

I am using Visual C# Express Edition 2010.


回答1:


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!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. MyDatabase)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database 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...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.




回答2:


Change the Copy to output directory setting from Copy always to Copy if newer in the property page for your database files. Every time you run the application the new database file is copied from your project to the bin folder, so all of your datas from the previous run are removed.

More: https://msdn.microsoft.com/en-us/library/ms246989.aspx




回答3:


I figured out a pretty simple answer but maybe it's not the best use to do it this way. Anyway, I add the database to the project and then I connect directly to it by doing so:

string path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory)));
SqlConnection c = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=""" + path + @"Database1.mdf"";Integrated Security=True;User Instance=True");

This way, if you move your project between multiple PCs, the connection will still work (I needed this the most). This also works with .SDF files and it's easier too.



来源:https://stackoverflow.com/questions/36245933/all-data-from-database-is-deleted-when-closing-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!