LocalDB MDF文件属性如何“如果更新则复制"作品

问题描述:

我正在使用.NET Framework 4.5在Visual Studio 2012中开发我的应用程序,并将MDF文件用作本地数据库.这里的问题是在运行时对数据所做的更改不会永久保存.但是,我能够在运行时检索数据甚至将其保存,但它们不会显示在Server Explorer中.我已经将MDF文件的复制到输出目录"属性设置为如果更新则复制",但是问题仍然存在.我该如何解决?

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?

这是我的应用程序的app.config中的连接字符串.我之前曾尝试使用单独的数据集.xsd文件,但无济于事.

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>

有些人可能会建议:

  1. 将SQLExpress用作服务器,而不是LocalDB或
  2. 尝试以编程方式连接到LocalDB并替换bin \ Debug 与"folder_name \ file_name.mdf"
  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?

根据要求,C#代码为:

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.

在此问题上,我从@SamiKuhmonen和@BrendanGreen获得了帮助,解决方案是我正在通过服务器资源管理器检查项目文件夹的MDF文件中是否存在数据. ,我应该做的是检查bin \ Debug文件夹中的MDF文件,其中我的数据是永久的.我是通过在服务器资源管理器中创建一个新连接以输出MDF文件来实现的.我错误地认为更新的MDF文件将覆盖项目文件夹中的文件,因为它应该是如果较新则进行复制"的功能.实际上,它会将原始文件复制到bin文件夹,而不是其他方式.很明显,MDF填充对于全新安装将为空,并且在部署之后,用户端的应用程序将仅引用已部署的文件,即类似于bin \ Debug中的文件.

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.