0

有人可以帮我弄清楚如何让 VB.NET 更新其连接的 MS ACCESS 数据库吗?

MS Access 2013 Visual Studio 2012(框架 4.0)

问题:点击“保存”按钮后,我的 DataGrid 不会更新其源 MS Access 数据库。这只是

基本上,我只是以标准方式建立连接。这是我失败结构的一个非常通用和基本的版本。

Imports System.Data.OleDb

Public Class Form1

Private Sub StudentBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles StudentBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.StudentBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.TestdbDataSet)

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'TestdbDataSet.Student' table. You can move, or remove it, as needed.
    Me.StudentTableAdapter.Fill(Me.TestdbDataSet.Student)

End Sub

End Class

这只是安装向导中系统生成的代码。在过去的 8 个小时里,我一直在尝试解决这个看似良性的问题。我只是累坏了。

4

4 回答 4

0

Community,

After some additional research and a lot of trial & error, I have figured out how to get vb.NET to update its MS ACCESS Database. Basically, you need to change one property of the imported database, and change the update command of the imported dataset. Here is a complete walkthrough of the steps.

►MS Access 2013
►Visual Studio 2012
►.NET Framework 4.5

(For best results, follow each step deliberately).

Step 1: Create Sample MS Access Database

  • Table Name:** "Table A"
  • Field 1: "ID" | [Autonumber Product Key]
  • Field 2: "Title" | "Sample A", "Sample B", ... "Sample G"
  • Field 3: "Val" | "a", "b", ..., "g"
  • Save As: "SampleAccessDatabase.mdb"
  • Close: Close MS Access

Step 2: Open Visual Studio 2012 Project

  • New Project: Templates ► Visual Basic ► "Windows Form Application"

Step 3: Connect to MS Access Database

  • Add New Data Source ► Database [Next>] ► Database [Next>] ► New Connection
  • Add Connection Window: Data Source ► "Microsoft Access Database File (OLE DB)"
  • Add Connection Window: Database File Name ► Browse ► Grab "SampleAccessDatabase.mdb" file ► [Hit OK]
  • Add Connection Window: [Hit OK] ► [Next>] ► [Hit Yes] ► [Next >]
  • Choose Your Database Objects: Select "Table A" ► Finish

Step 4: Place Control Objects

  • Data Sources: Grab "Table A" and place it on the "Form1" object

Step 5: Change DataSet Property

  • Solution Explorer: Select "SampleAccessDatabase.mdb"
  • Properties: Select "Copy To Output Directory" ► Change to "Copy if newer"

Step 6: Change TableAdapter Update Command

  • Solution Explorer: Double-Click "SampleAccessDatabaseDataset.xsd"
  • Pane: Select "Table ATableAdapter" Object in the middle pane
  • Properties: Expand [Update Command] by clicking on dropdown menu and selecting [New]
  • Update Command: Open [CommandText] by clicking on the ellipsis [...]
  • Add Table: Select "Table A" and hit [Add]
  • Query Builder: After the word SET type "Field2=?". Hit Enter
  • Query Builder: After the word "WHERE" type "(Field1=?". [Hit OK]

Step 7: Test it out

  • CTRL+F5: Change "Field2" to "1","2",..."7" ► Close Form/Application
  • CTRL+F5: View updated DataGrid

That is it. Now, you can create your .NET Desktop application in Visual Studio 2012 with the ability to store & update data from your MS Access database (2013).

于 2013-08-09T04:29:53.100 回答
0

如果您使用 Visual Studio 上的开始按钮启动应用程序,它会将新数据库复制到您的应用程序文件(存储在 "/debug/youraplication.exe" 中)。尝试在调试文件夹中直接执行 youraplication.exe,然后尝试使用您的应用程序插入/删除/更新您的数据库,然后关闭您的应用程序,现在打开存储在 debug 文件夹中的 databasae.accdb,现在您可以看到您的数据库包含您的输入

于 2014-02-09T09:41:01.680 回答
0

这可以解决问题:

第 5 步:更改数据集属性

解决方案资源管理器:选择“SampleAccessDatabase.mdb”属性:选择“复制到输出目录”► 更改为“如果较新则复制”

我的问题是应用程序总是将新数据库复制到应用程序的调试文件夹...

于 2018-02-21T14:59:21.973 回答
0

我仍然有这个问题,无法弄清楚希望像你一样“强制”更新。到目前为止,似乎我拥有的每个数据集都至少有一个以编程方式填写的字段,并且没有关联的控件,用户可以在表单上编辑该字段,数据集不允许更新/删除,只能新建和插入。不过,我可能走错了路。

于 2015-09-29T16:29:52.857 回答