这是插入数据库微软访问的代码, 但是出现错误。说插入错误 请告诉我为什么会这样。并告诉我如何解决这个问题。
感谢
首先从文本框中收集数据 然后代码选择数据表 然后放入数据集 之后插入数据库。 我没有看到问题
If txtCode.Text = "" Then
txtCode.Focus()
ElseIf cmbCneeName.Text = "" Then
cmbCneeName.Focus()
ElseIf txtCneeAddress1.Text = "" Then
txtCneeAddress1.Focus()
ElseIf txtCneeAddress2.Text = "" Then
txtCneeAddress2.Focus()
Else
Dim acces_da As OleDbDataAdapter
Dim cm As OleDbCommand
Dim acces_ds As DataSet
Dim datarowtblshipper As DataRow
Dim cmdbuilder As OleDbCommandBuilder
cm = New OleDbCommand
With cm
.Connection = DBconnection()
.CommandText = "select * from TblConsignee where con_code='" & txtCode.Text & "'"
End With
acces_da = New OleDbDataAdapter
acces_ds = New DataSet
acces_da.SelectCommand = cm
cmdbuilder = New OleDbCommandBuilder(acces_da)
acces_da.Fill(acces_ds)
Try
datarowtblshipper = acces_ds.Tables(0).NewRow
datarowtblshipper("Con_Code") = txtCode.Text
datarowtblshipper("Con_Comp") = cmbCneeName.Text
datarowtblshipper("Con_Add1") = txtCneeAddress1.Text
datarowtblshipper("Con_Add2") = txtCneeAddress2.Text
datarowtblshipper("Con_Add3") = txtCneeAddress3.Text
datarowtblshipper("Con_Add4") = txtCneeAddress4.Text
datarowtblshipper("Con_PCode") = txtCneePostCode.Text
datarowtblshipper("Con_tel") = txtCneePhoneNo.Text
datarowtblshipper("Contents") = txtCneeContents.Text
datarowtblshipper("destination") = txtDestination.Text
datarowtblshipper("Con_Cont") = txtAttention.Text
datarowtblshipper("zone") = tzone.Text
acces_ds.Tables(0).Rows.Add(datarowtblshipper)
cmdbuilder.GetInsertCommand()
acces_da.Update(acces_ds)
MsgBox("New Data Inserted!")
Catch
MsgBox("Error occured : " & vbCrLf & Err.Description, vbExclamation, "error")
End Try
End If
答案 0 :(得分:1)
我定期使用Access数据库。不幸的是,我对你使用的方法并不熟悉,因为我还不熟悉VB.NET。
尝试使用此方法,可以更轻松地跟踪错误:
Using con As New OleDbConnection
con.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; " & _
"Data Source = " '''Your path here
Dim sql_insert As String = "INSERT INTO TblConsignee (Con_Code, Con_Comp, Con_Add1, " & _
"Con_Add2, Con_Add3, Con_Add4, Con_PCode, Con_tel, " & _
"Contents, destination, Con_Cont, zone) " & _
"VALUES " & _
"(@code, @comp, @add1, @add2, @add3, @add4, @pcode, " & _
"@tel, @content, @dest, @zone);"
Dim sql_insert_entry As New OleDbCommand
con.Open()
With sql_insert_entry
.Parameters.AddWithValue("@code", txtCode.Text)
.Parameters.AddWithValue("@comp", cmbCneeName.Text)
.Parameters.AddWithValue("@add1", txtCneeAddress1.Text)
.Parameters.AddWithValue("@add2", txtCneeAddress2.Text)
.Parameters.AddWithValue("@add3", txtCneeAddress3.Text)
.Parameters.AddWithValue("@add4", txtCneeAddress4.Text)
.Parameters.AddWithValue("@pcode", txtCneePostCode.Text)
.Parameters.AddWithValue("@tel", txtCneePhoneNo.Text)
.Parameters.AddWithValue("@content", txtCneeContents.Text)
.Parameters.AddWithValue("@dest", txtDestination.Text)
.Parameters.AddWithValue("@cont", txtAttention.Text)
.Parameters.AddWithValue("@zone", tzone.Text)
.CommandText = sql_insert
.Connection = con
.ExecuteNonQuery()
End With
con.Close()
End Using
如果要更新记录,只需更改命令字符串即可更新数据库的所需字段。