问题描述
你好,我有一个数据库,我在 vb.net 应用程序中加载到 Datagridview.它加载良好,但是当我尝试保存日期时它不起作用.这是代码
Hello i've a databse that i load to Datagridview in vb.net application.it loads fine, however when i try to save the date it doesn't work.here is the code
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 集合时,更新需要有效的 InsertCommand.我该怎么办?
Update requires a valid InsertCommand when passed DataRow collection with new rows.what am i supposed to do?
accessdatabase 3 列和 ID 为主键ID 字段 1 字段 2
the accessdatabase 3 colmuns and ID is primary keyID Field1 Field2
推荐答案
所以你必须定义一个 InsertCommand
为你DataAdapter
旁注:DSet.AcceptChanges()
行是多余的,因为前一行 Dadapter.Update
将隐式调用 AcceptChanges
.
Side-note: The line DSet.AcceptChanges()
is redundant since the previous line Dadapter.Update
will call AcceptChanges
implicitely.
您应该将 using-statement
用于任何实现 IDisposable
的东西,例如 Connection.即使发生异常,这也会隐式调用 Dispose
(关闭连接).
You should use using-statement
for anything implementing IDisposable
like a Connection. That would call Dispose
(which closes the connection) implicitely even in case of an exception.
所以替换:
con.Open()
Dadapter.Update(DSet, "Table1")
DSet.AcceptChanges()
con.Close()
与
Using con = New OleDbConnection(myConString)
con .Open()
Dadapter.Update(DSet, "Table1")
End Using
这篇关于Datagridview 将更改保存到数据库 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!