我的 Windows 窗体应用程序中的 Sqlcompact 数据库出现问题
我已经针对我的问题进行了很多搜索,但没有解决方案,因此我在这里发帖以获取专家的建议。
我正在制作一个 Windows 窗体应用程序,并使用 Sql 紧凑数据库。
我的问题:
当我使用代码(运行时)在数据库中插入记录时,它们被插入、删除、更新,并且我可以看到它们在我的应用程序中更新、插入、删除,但是当我关闭应用程序并仅重新启动几次时它们被改变了,一百万次:)。否则我会再次看到空白数据库,或者如果我直接将解决方案资源管理器 Visual studio 2010 中的值放入数据库中,它们将永远保留在那里。我正在使用简单的更新、插入、删除语句,但不确定为什么当我关闭应用程序时对数据库所做的更改会消失。
首先,对于所有人:
Dim con As New SqlCeConnection
Dim command As New SqlCeCommand
connectionString = "Data Source=SqlCompactDatabase/RestaurantDatabase.sdf"
con.ConnectionString = connectionString
command.Connection = con
con.Open()
我的插入代码:
command.CommandText = "Insert into Inventory(itemcode,itemmin,itemmax,itemdesc) Values('" + itmcod + "','" + min + "','" + max + "','" + desc + "')"
command.ExecuteScalar()
我的更新代码:
command.CommandText = "update Inventory set itemMin='" + min + "',itemMax='" + max + "',instock='" + stock + "',itemDesc='" + desc + "' where itemcode='" + TextBox1.Text + "'"
command.ExecuteScalar()
我的删除代码:
command.CommandText = "delete from inventory where itemcode='" + TextBox1.Text + "'"
command.ExecuteScalar()
我正确打开关闭连接,没有错误,有一些我无法弄清楚的东西:)
I have searched a lot regarding my problem but no solution so i am posting here to get advise from the experts.
I am making a windows form application , and using Sql compact database.
My Problem :
When i am inserting records in the database using code (run time) they are inserted deleted , update and i can see them updated,inserted,deleted in my application , but when i close the application and start again only a few times they are changed, once a million :). Else i again see blank database ,or if i directly put values in the database from the solution explorer Visual studio 2010 , they stay there forever. I am using simple update,insert ,delete statement but not sure why the changes made to database disappear when i close the application.
First this For all :
Dim con As New SqlCeConnection
Dim command As New SqlCeCommand
connectionString = "Data Source=SqlCompactDatabase/RestaurantDatabase.sdf"
con.ConnectionString = connectionString
command.Connection = con
con.Open()
My Insert Code :
command.CommandText = "Insert into Inventory(itemcode,itemmin,itemmax,itemdesc) Values('" + itmcod + "','" + min + "','" + max + "','" + desc + "')"
command.ExecuteScalar()
My Update Code:
command.CommandText = "update Inventory set itemMin='" + min + "',itemMax='" + max + "',instock='" + stock + "',itemDesc='" + desc + "' where itemcode='" + TextBox1.Text + "'"
command.ExecuteScalar()
My Delete Code:
command.CommandText = "delete from inventory where itemcode='" + TextBox1.Text + "'"
command.ExecuteScalar()
I open close connection correctly there is no error, there is something i can't figure out :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将 SQLCE 数据库添加到 Visual Studio 中的项目中,请查看其属性(在解决方案资源管理器中选择文件并查看“属性”面板)。通常,数据库文件的“构建操作”设置为“内容”,“复制到输出”选项设置为“始终复制”。这意味着,每次构建解决方案时,Visual Studio 项目中的数据库(很可能是空项目)都会重新复制到构建文件夹($projectname$\bin\Debug 或 \Release),并使用数据覆盖现有数据库在运行时添加。
要解决此问题,只需将“复制到输出”设置为“如果较新”,只要设计数据库文件发生更改,它就会覆盖您的数据。
If you added you SQLCE database to your project in Visual Studio, then look at its properties (select the file in the Solution Explorer and look to the Properties panel). Normally a database file has Build Action set to "Content" and Copy To Output option set to "Copy Always". This means, that every time you build your solution your database from Visual Studio project (most likely the empty one) is copied anew to your build folder ($projectname$\bin\Debug or \Release) and overwrites your existing database with the data added at runtime.
To solve this problem just set Copy To Output to "If newer" and it will overwrite your data as long as your design-database file changes.