LocalDB MDF文件属性“如果更新则复制”不起作用 [英] LocalDB MDF file property “Copy if newer” not working

查看:81
本文介绍了LocalDB MDF文件属性“如果更新则复制”不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的应用程序的app.config中的连接字符串。我之前尝试过Seperate数据集.xsd文件和相关的DataAdapter来保存数据但无济于事。



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 saved permanently. 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 and associated DataAdapters to save data 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



然后它们不是实际的解决方案,只是替代方案。有谁能解释这里发生了什么?我该如何解决?



C#代码是:






Some might suggest:
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 alternatives. Can anyone please explain what is happening here? and how do I solve it?

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.

推荐答案

我收到了一些人对这个问题的帮助,解决方法是我通过Server Explorer检查项目文件夹的MDF文件中是否存在数据,我应该做什么而是检查我的数据是永久性的bin \Debug文件夹中的MDF文件。我通过在服务器资源管理器中创建一个新连接来输出MDF文件。我错误地认为更新的MDF文件将覆盖项目文件夹中的文件,因为它应该是如果更新则复制的功能。实际上它将原始文件复制到bin文件夹而不是其他方式。很明显,对于全新安装,MDF填充将为空,在部署之后,用户端的应用程序将仅引用已部署的文件,即类似于bin \Debug中的文件。
I received help from some people on this problem 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.


这篇关于LocalDB MDF文件属性“如果更新则复制”不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆