为什么我的数据库没有更新? [英] Why is my database not updating?
问题描述
我使用的代码在下面。
公共类Form9
继承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)_
处理MyBase.Load
conn.ConnectionString =Provider = Microsoft.ACE.OLEDB.12.0; Data Source ='& _
文件夹路径& \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
结束类
你必须更新底层的 DataTable
。 DataGridView.Update
导致控件重绘其客户区中的无效区域(基本上是重绘)。 dt.AcceptChanges()
仅提交DataTable中的更改,而不是数据库。必须使用适配器和适当的命令文本显式更新数据库。 OleDbCommandBuilder
有助于形成适当的命令文本。
使用 OleDbDataAdapter
更新数据库。
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
cb.QuotePrefix =[;
cb.QuoteSuffix =];
adapter.Update(datatable);
My problem is that when I am editing cells in the datagrid the database is not updating. The code I used is below.
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
You have to update the underlying DataTable
. The DataGridView.Update
causes the control to redraw the invalidated regions within its client area (basically repaints). The dt.AcceptChanges()
only commits the changes in the DataTable not the database. The database has to be updated explicitly using the adapter and appropriate command texts. The OleDbCommandBuilder
helps in forming the appropriate command texts.
Use the OleDbDataAdapter
to update the database.
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
cb.QuotePrefix = "[";
cb.QuoteSuffix = "]";
adapter.Update(datatable);
这篇关于为什么我的数据库没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!