gpt4 book ai didi

database - Datagridview 将更改保存到数据库 vb.net

转载 作者:搜寻专家 更新时间:2023-10-30 19:43:53 25 4
gpt4 key购买 nike

你好,我有一个数据库,我在 vb.net 应用程序中加载到 Datagridview。它加载正常,但是当我尝试保存日期时它不起作用。这是代码

    Private myConString As String
Private con As OleDbConnection = New OleDbConnection
Private Dadapter As OleDbDataAdapter
Private DSet As DataSet
Private DSet2 As DataSet
Private ConCMD As OleDb.OleDbCommand

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=e:\Database31.accdb"
con.ConnectionString = myConString
con.Open()
Dadapter = New OleDbDataAdapter("select * from Table1", con)
DSet = New DataSet
Dadapter.Fill(DSet, "Table1")
DataGridView1.DataSource = DSet.Tables("Table1")
con.Close()
End Sub


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
con.Open()
Dadapter.Update(DSet, "Table1")
DSet.AcceptChanges()
con.Close()
End Sub

当传递带有新行的 DataRow 集合时,Update 需要一个有效的 InsertCommand。我该怎么办?

accessdatabase 3列,ID为主键ID Field1 Field2

最佳答案

所以你必须定义一个InsertCommand为你 DataAdapter

旁注:DSet.AcceptChanges() 行是多余的,因为上一行 Dadapter.Update 将隐式调用 AcceptChanges

您应该对任何实现 IDisposable 的东西使用 using-statement,例如 Connection。即使在出现异常的情况下,这也会隐含地调用 Dispose(关闭连接)。

所以替换:

con.Open()
Dadapter.Update(DSet, "Table1")
DSet.AcceptChanges()
con.Close()

Using con =  New OleDbConnection(myConString)
con .Open()
Dadapter.Update(DSet, "Table1")
End Using

关于database - Datagridview 将更改保存到数据库 vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com