How LocalDB MDF file property “Copy if newer” works

心已入冬 提交于 2020-01-30 11:10:07

问题


I am developing my application in Visual Studio 2012 with .NET Framework 4.5 and using an MDF file as local db. Problem here is changes made to data at runtime are not getting saved permanently. However, I am able to retrieve data at runtime even save it but they are not displayed in Server explorer. I have already set the "Copy to Output directory" property of MDF file to "Copy if newer" but problem still exists. How do i solve it?

This is the connection string in my application's app.config. I had earlier tried Seperate data set .xsd file but to no avail.

<connectionStrings>
<add name="EA_Logistics_Control.Properties.Settings.LogisticsLocalDBCS"
  connectionString="Data Source=(LocalDB)\v11.0
AttachDbFilename=|DataDirectory|\LocalDBs\LogisticsLocalDB.mdf;Integrated
Security=True"
  providerName="System.Data.SqlClient" />
</connectionStrings>

Some might suggest to :

  1. Use SQLExpress as server instead of LocalDB or
  2. Try to connect to LocalDB programmatically and replace the bin\Debug with "folder_name\file_name.mdf"

but then they are not actual solutions, just alternative. Can anyone please explain what is happening here? and how do I solve it?

As requested, C# code is:

DateTime dt = DateTime.Now;
string CDate = dt.Month + "/" + dt.Day + "/" + dt.Year;
int norid = 0, neeid = 0, cfrom = 0, cto=0;

SqlConnection SCon = new SqlConnection(ConfigurationManager.ConnectionStrings["EA_Logistics_Control.Properties.Settings.LogisticsLocalDBCS"].ConnectionString);

if (SCon.State == ConnectionState.Closed)
    SCon.Open();
SqlCommand SCom = new SqlCommand("INSERT INTO Consignments VALUES(@CNo, @CDate, @InvoiceNo, @SignorID, @SigneeID, @TravelFrom, @TravelTo, @Packages, @Contents, @VehicleNo, @Rate, @Weight, @Amount, '')", SCon);

SCom.Parameters.AddWithValue("CNo", TB_IC_CN.Text);
SCom.Parameters.AddWithValue("CDate", CDate);
SCom.Parameters.AddWithValue("InvoiceNo", TB_IC_O_Inv.Text);
SCom.Parameters.AddWithValue("SignorID", norid);
SCom.Parameters.AddWithValue("SigneeID", neeid);
SCom.Parameters.AddWithValue("TravelFrom", cfrom);
SCom.Parameters.AddWithValue("TravelTo", cto);
SCom.Parameters.AddWithValue("Packages", TB_IC_NUM_O_Pkgs.Text);
SCom.Parameters.AddWithValue("Contents", TB_IC_Desc.Text);
SCom.Parameters.AddWithValue("VehicleNo", TB_IC_O_Veh.Text);
SCom.Parameters.AddWithValue("Rate", TB_IC_NUM_O_Rate.Text);
SCom.Parameters.AddWithValue("Weight", TB_IC_NUM_O_Wt.Text);
SCom.Parameters.AddWithValue("Amount", TB_IC_NUM_Amt.Text);
int resRows = SCom.ExecuteNonQuery();
if (resRows > 0)
{
    MessageBox.Show("New entry has been inserted.");
}

It does show me a message box and I have also seen number of rows affected while debugging.


回答1:


I received help from @SamiKuhmonen and @BrendanGreen on this matter and solution was that I was checking for existence of data in MDF file of my project folder through Server Explorer, what I should have done instead was to check the MDF file in bin\Debug folder where my data were permanent. I did it by creating a new connection in Server Explorer to output MDF file. I wrongly assumed that updated MDF file will overwrite the one in project folder as it is supposed to be feature of "Copy if newer". Actually it copies original file to bin folder rather than other way around. Quite obviously, the MDF fill will be empty for fresh installation and after deployment, application on user side will only refer to the deployed file i.e. similar to the one in bin\Debug.



来源:https://stackoverflow.com/questions/29943900/how-localdb-mdf-file-property-copy-if-newer-works

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