vb.net 简单的 VB 2010 应用程序不将数据保存到 Access 2007
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13513123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple VB 2010 application not saving data to Access 2007
提问by user961627
I've downloaded the trial version of VB 2010 and made a small application that connects to an Access 2007 MDB file.
我已经下载了 VB 2010 的试用版,并制作了一个连接到 Access 2007 MDB 文件的小应用程序。
A couple of things are going wrong:
有几件事出了问题:
If I try to publish it, and then run the setup file generated, it says it can't find the .MDB file.
If I run it during development (F5), it runs fine, and when I enter new data into a DataGridView, I know it gets saved because when I close the session and hit F5 again, the newly entered data is still present. (The relevant code for updating the data is simple enough):
Me.Validate() Me.MenuItemsBindingSource.EndEdit() Me.MenuItemsTableAdapter.Update(Me.MenuOrdersDataSet.MenuItems) Me.MenuOrdersDataSet.AcceptChanges()
如果我尝试发布它,然后运行生成的安装文件,它会说找不到 .MDB 文件。
如果我在开发 (F5) 期间运行它,它运行良好,当我将新数据输入到 DataGridView 中时,我知道它会被保存,因为当我关闭会话并再次按 F5 时,新输入的数据仍然存在。(更新数据的相关代码很简单):
Me.Validate() Me.MenuItemsBindingSource.EndEdit() Me.MenuItemsTableAdapter.Update(Me.MenuOrdersDataSet.MenuItems) Me.MenuOrdersDataSet.AcceptChanges()
But if I close the whole project and rerun it and view the DataGridView, or if I manually go and check out the .MDB file, it no longer has the newly entered data.
但是如果我关闭整个项目并重新运行它并查看 DataGridView,或者如果我手动去检查 .MDB 文件,它不再有新输入的数据。
This is my connection code:
这是我的连接代码:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MenuItemsTableAdapter.Fill(Me.MenuOrdersDataSet.MenuItems)
Dim con As New OleDb.OleDbConnection
Dim dbString As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MenuOrders.accdb"
con.ConnectionString = dbString
con.Open()
sql = "SELECT * FROM MenuItems"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "MenuItems")
con.Close()
End Sub
What's the matter? Are these problems related to having a trial version, or are there some other obvious problems I should be aware of?
怎么了?这些问题是否与试用版有关,或者还有其他一些我应该注意的明显问题?
回答by Sam Nakale
I used to have the same problem, and I think I fixed it by setting my data source/ database Copy to Output Directoryto "Copy if newer", located in the properties window for the MyDatabase.mdf file.
我曾经遇到过同样的问题,我想我通过将数据源/数据库复制到输出目录设置为“如果更新则复制”来修复它,该目录位于 MyDatabase.mdf 文件的属性窗口中。
Sometimes the option can default to "Always" which explains why the data set resetsevery time you run the application.
有时该选项可以默认为“始终”,这解释了为什么每次运行应用程序时数据集都会重置。
回答by user961627
Finally did the following to fix it.
最后做了以下修复。
In Solution Explorer I opened App.config and put the absolute path to the database in the connectionStringstag, replacing the default |DataDirectory|text.
在解决方案资源管理器中,我打开 App.config 并将数据库的绝对路径放在connectionStrings标记中,替换默认|DataDirectory|文本。
Not sure if this is the best thing to do..but it worked!
不确定这是否是最好的做法……但它奏效了!
<connectionStrings>
<add name="MenuSystem.My.MySettings.MenuDBConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Documents\MenuDB.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>

