简体   繁体   English

如何使用SQL Server CE和C#更新dB表中的单元格值(Visual Studio 2010)

[英]How can I update a cell value in a dB table, using SQL Server CE and C# (Visual Studio 2010)

I'm writing a small application for college, a video rental application. 我正在为大学写一个小应用程序,一个视频租赁应用程序。 I have no problems reading from the database, but I cannot update the data in the tables. 我从数据库中读取没有问题,但我无法更新表中的数据。

In particular I'm looking to update the cell showing the quantity of a film in stock after someone rents a movie. 特别是我想在有人租一部电影后更新显示有关电影数量的单元格。

So far I've been trying this: 到目前为止,我一直在尝试这个:

string updateDVDs = "UPDATE Products SET dvd_quantity = " + product.Quantity + " WHERE title = '"+ product.Name +"';";
cmdUpdateDVDs = new SqlCeCommand(updateDVDs, dBConnection);
dBConnection.Open();
cmdUpdateDVDs.ExecuteNonQuery();
dBConnection.Close();

I don't get any errors, but the cell doesn't update in the table. 我没有收到任何错误,但表格中的单元格没有更新。 Any help would be greatly appreciated, please let me know if you require further information. 如有任何帮助将不胜感激,如果您需要更多信息,请告诉我。

UPDATE: OK, I have some interesting developments, I have updated the code to this: 更新:好的,我有一些有趣的发展,我已经更新了代码:

string updateDVDs = "UPDATE Products SET dvd_quantity = " + product.Quantity + " WHERE title = '" + product.Name + "'";
dBConnection = new SqlCeConnection(connectionString);
cmdUpdateDVDs = new SqlCeCommand(updateDVDs, dBConnection);
cmdUpdateDVDs.Connection.Open();
int rows = cmdUpdateDVDs.ExecuteNonQuery();
dBConnection.Close();

From what I can tell looking on MSDN both methods are valid, but this second one seems to be working. 从我在MSDN上看到的两种方法都是有效的,但第二种方法似乎有效。 Now the weird bit, I rent the movie 'Die Hard', it all goes swimmingly, I get these results: 现在奇怪的是,我租了电影'死硬',这一切都顺其自然,我得到了这些结果:

数据库已更新

All good, There is no way back from this point, so I quit the application and start again. 一切都很好,从这一点开始就没有办法了,所以我退出了应用程序并重新开始。 I go to rent 'Die Hard' a second time to confirm, and, success!! 我第二次去'Die Hard'租房来确认,并且,成功!! the new reading on the DVD quantity is 0 as expected: DVD数量的新读数为0,如预期: DVD数量已成功更新!

But, when I open the Product table in Visual Studio the original values are still there: 但是,当我在Visual Studio中打开Product表时,原始值仍然存在: 在Visual Studio中,表尚未更新

Not only that, but when I run the application again after opening the table in Visual Studio the DVD quantities are reset to the original values and the updated values are gone. 不仅如此,当我在Visual Studio中打开表格后再次运行应用程序时,DVD数量将重置为原始值,更新后的值将消失。

Am I missing something simple here? 我错过了一些简单的东西吗? I've tried refreshing the table, it doesn't make any difference. 我试过刷新表,它没有任何区别。 As long as I don't open the table in Visual Studio the application behaves as expected, no matter how many times I run it, the values update as expected, until I open the table itself. 只要我不在Visual Studio中打开表,应用程序就会按预期运行,无论我运行多少次,值都会按预期更新,直到我打开表本身。

You have to tell us exactly what connection string you are using, saying things like "I updated the connection to point to the database in the bin folder" is not helpful. 你必须告诉我们你正在使用什么连接字符串,说“我更新连接指向bin文件夹中的数据库”这样的东西没有用。 Are you using a connection string like c:\\Users\\Me\\Visual Studio 2010\\Projects\\MyProject\\bin\\debug\\HKRMoviesDB.sdf ? 您使用的连接字符串如c:\\Users\\Me\\Visual Studio 2010\\Projects\\MyProject\\bin\\debug\\HKRMoviesDB.sdf That is not right, that is hard-coded to your VS2010 debug test folder on your machine, and will not work for anyone else, will only work when you run from the debugger, and will not work when you deploy your app. 这是不对的,它是硬编码到您机器上的VS2010调试测试文件夹,并且对其他任何人都不起作用,只有在您从调试器运行时才能工作,并且在您部署应用程序时无法工作。

ADO.NET looks for vertical bars in connection strings and expands the enclosed string using the AppDomain property. ADO.NET在连接字符串中查找垂直条,并使用AppDomain属性扩展包含的字符串。 For example, if you use Data Source=|DataDirectory|\\HKRMoviesDB.sdf from the VS debugger, DataDirectory expands to your project's bin folder, where VS copies the debug DLLs. 例如,如果从VS调试器使用Data Source=|DataDirectory|\\HKRMoviesDB.sdfDataDirectory将扩展到项目的bin文件夹,其中VS复制调试DLL。 If you deploy your app using an MSI installer, DataDirectory expands to your app's install folder chosen by the user. 如果使用MSI安装程序部署应用程序, DataDirectory将扩展到用户选择的应用程序安装文件夹。 Further explanation is at this answer . 这个答案进一步解释。

Be aware that DataDirectory may not be a directory writable by users; 请注意, DataDirectory可能不是用户可写的目录; it is a good directory for read-only data, but if you want users to write to the file you should copy it to Environment.SpecialFolder.ApplicationData or somewhere in your app, as explained in this answer . 它是一个用于只读数据的好目录,但是如果您希望用户写入该文件,则应将其复制到Environment.SpecialFolder.ApplicationData或应用程序中的某个位置,如本答案中所述

There is one more thing to keep in mind. 还有一件事需要记住。 When you are debugging you typically want to copy your original source data to the debug folder, test it, then discard your test data because you typically do not want your test data included in the final deployment. 在进行调试时,您通常希望将原始源数据复制到调试文件夹,对其进行测试,然后丢弃测试数据,因为您通常不希望测试数据包含在最终部署中。 This is what the "Copy to Output Directory" property in your project does. 这就是项目中“复制到输出目录”属性的功能。 If it is set to "copy if newer", which it probably should be, then any changes you make to your test data is temporary; 如果它设置为“如果更新则复制”,它可能应该是,那么您对测试数据所做的任何更改都是临时的; the next time you update your original source data in your project, your test data will be discarded. 下次更新项目中的原始源数据时,将丢弃测试数据。 Remember, your application's deployment directory is typically not writable by users so you should not be writing user data in DataDirectory anyway. 请记住,您的应用程序的部署目录通常不能由用户写入,因此您不应该在DataDirectory编写用户数据。

A fellow student pointed this out to me: 一位同学向我指出:

"From what I understand, the database that your code updates, unless you hard coded the path to the database is the file in your /bin/debug/ folder. The file you view in Visual studio is the one in your project root." “根据我的理解,您的代码更新的数据库,除非您硬编码数据库的路径是/ bin / debug /文件夹中的文件。您在Visual Studio中查看的文件是项目根目录中的文件。”

I updated the connection to point to the database in the bin folder. 我更新了连接以指向bin文件夹中的数据库。

暂无
暂无

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

相关问题 重命名Visual Studio 2010中C#解决方案中的SQL Server表 - Rename SQL Server Table in c# solution in Visual Studio 2010 如何使用C#确定SQL Server CE表中是否存在列? - How can I determine whether a column exists in a SQL Server CE table with C#? 如何在Visual Studio 2010中使用C#创建简单的SOAP服务器? - How do I create a simple SOAP server using C# in Visual Studio 2010? 检索存储在Visual Studio 2010中的SQL Server CE数据库中的信息 - Retrieving information stored in a SQL Server CE database in Visual Studio 2010 C#Visual Studio:如何使用LINQ使用DataGridView从Edit更新数据库? - C# Visual Studio: How do I update a DB from Edit with DataGridView using LINQ? 更新无法在SQL Server CE C#中工作 - Update not working in SQL Server CE C# 如何使用一些 RAD 工具或 Visual Studio 2010 在 C# winforms SQL 服务器中获取模板化的起始数据库驱动的 forms - How to get templated starter database driven forms generated in C# winforms SQL Server using some RAD tool or Visual Studio 2010 在尝试使用C#连接到Visual Studio 2010中的本地SQL Server数据库时遇到问题 - Having problems trying to connect to local SQL Server database in Visual Studio 2010 using C# 如何使用 Visual Studio 通过 C# 连接到 SQL 服务器实例 - How to connect to a SQL Server instance with C# using Visual Studio 如何将Visual Studio 2010 Express C#连接到SQL Server Express - How to connect Visual Studio 2010 Express C# to SQL Server Express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM