简体   繁体   English

C#SQL Server CE未插入

[英]C# SQL Server CE not inserting

SqlCeConnection sqlCnn = 
    new SqlCeConnection(Properties.Settings.Default.mainDBConnectionString);

SqlCeCommand sqlCmd = new SqlCeCommand(
    "INSERT INTO desktopItems (Location,Label) VALUES (@Location, @Label)", 
    sqlCnn);

sqlCnn.Open();
sqlCmd.Parameters.Add("@Location", openExe.FileName.ToString());
sqlCmd.Parameters.Add("@Label", openExe.SafeFileName.ToString());
sqlCmd.ExecuteNonQuery();

sqlCnn.Close();

I have this code but when I run the program, the database is not updating ... 我有这个代码但是当我运行程序时,数据库没有更新...

Usually this scenario is caused by a simple error in visualizing the database. 通常这种情况是由可视化数据库中的简单错误引起的。
Your INSERT works as expected, but you check if the insert succeded looking at a database in the wrong directory. 您的INSERT按预期工作,但您检查插入是否成功查看错误目录中的数据库。

Using the DATADIRECTORY substitution string with a WinForms application means that, at debug time, your database is expected to be located in the directory BIN\\DEBUG from your base project folder. DATADIRECTORY替换字符串与WinForms应用程序一起使用意味着,在调试时,您的数据库应位于基础项目文件夹的目录BIN\\DEBUG
Visual Studio make sure that this is the case because in your project, the database file, is marked with the property Copy To The Output Directory set to Copy Always or Copy If Newer . Visual Studio确保是这种情况,因为在您的项目中,数据库文件标记为“ Copy To The Output Directory ”属性设置为Copy Always或“ Copy If Newer

And it is here that the insert happens when you run your code inside a debug session of Visual Studio. 当您在Visual Studio的调试会话中运行代码时,就会发生插入。

Then you check the result of the execution using the SERVER EXPLORER connection. 然后使用SERVER EXPLORER连接检查执行结果。 But this connection points to the original database in the Project Folder and, of course, the new record is not present. 但是此连接指向项目文件夹中的原始数据库,当然,新记录不存在。

Usually the database in the project folder is kept up to date for the deployement, with the correct schema and initial data, but without any records that are inserted just for debug purpose. 通常,项目文件夹中的数据库使用正确的模式和初始数据保持最新的部署,但没有任何仅为调试目的而插入的记录。

So you could simply add a new connection to the SERVER EXPLORER pointing to the database in the BIN\\DEBUG, rename it (like 'DEBUG-DB') and keep your original connection in case you need to change something in the schema of the database before releasing your application. 因此,您可以简单地向指向BIN \\ DEBUG中的数据库的SERVER EXPLORER添加新连接,重命名它(如'DEBUG-DB')并保留原始连接,以防您需要更改数据库模式中的某些内容在发布申请之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM