我正在使用http://homeandlearn.co.uk/NET/nets12p9.html的代碼添加記錄到數據庫。 它使用命令生成器時說,我不應該得到錯誤消息:VB.NET錯誤添加記錄到數據庫
更新需要一個有效的InsertCommand與新行傳遞DataRow集合時。
但是,當我做更新時,我仍然收到錯誤消息。我怎樣才能解決這個問題?
這是我的代碼:
Dim dbProv As String
Dim dbSource As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim Command As OleDb.OleDbCommand
Dim dr As DataRow
Dim cb As New OleDb.OleDbCommandBuilder(da)
sql = "SELECT * FROM Cliënten"
dbProv = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = [mydatabase]"
con.ConnectionString = dbProv & dbSource
con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Cliënten")
dr = ds.Tables("Cliënten").NewRow()
dr.Item("Field1") = TextBox1.Text
dr.Item("Field2") = TextBox2.Text
ds.Tables("Cliënten").Rows.Add(dr)
da.Update(ds, "Cliënten")
MsgBox("New Record added to the Database")
con.Close()
我覺得CommandBuilder的類應該根據SELECT自動生成INSERT ... –