gpt4 book ai didi

vb.net - 为什么我的数据库没有更新?

转载 作者:行者123 更新时间:2023-12-01 23:59:23 25 4
gpt4 key购买 nike

我的问题是,当我在数据网格中编辑单元格时,数据库没有更新。我使用的代码如下。

Public Class Form9
Inherits System.Windows.Forms.Form
Dim sql As String = "SELECT * FROM User_Account WHERE IsAdmin=False"
Dim conn As New OleDb.OleDbConnection
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql, conn)
Dim da As New OleDb.OleDbDataAdapter(sqlCom)
Dim dt As New DataTable

Private Sub Form9_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles MyBase.Load

conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & _
folderpath & "\TKIC\TKIC_Data_Storage.accdb'" & _
";Persist Security Info=True"
conn.Open()
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
DataGridView1.Update()
dt.AcceptChanges()
End Sub
End Class

最佳答案

您必须更新基础 DataTableDataGridView.Update 导致控件重绘其工作区内的无效区域(基本上是重绘)。 dt.AcceptChanges() 仅提交 DataTable 而非数据库中的更改。必须使用适配器和适当的命令文本显式更新数据库。 OleDbCommandBuilder 有助于形成适当的命令文本。

使用 OleDbDataAdapter 更新数据库。

OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
cb.QuotePrefix = "[";
cb.QuoteSuffix = "]";
adapter.Update(datatable);

关于vb.net - 为什么我的数据库没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22281938/

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