【发布时间】:2020-07-21 05:48:51
【问题描述】:
此代码在我的程序中的其他地方工作,但在这种形式下它不再工作。我有错误
System.Data.OleDb.OleDbException: 'Syntax error in INSERT INTO statement.'
这个错误发生在这一点
da.Update(ds, "EmployeeTable")
其余代码如下
Imports System.Data.OleDb
'Allows an connection of a type of Database to VB
Public Class frmAdmin
ReadOnly dbConnection As New OleDb.OleDbConnection
'Declares a variable that will handle the connection to Visual Basic
ReadOnly ds As New DataSet
'Declares the variable that will handle the datasets from the record
Dim da As OleDb.OleDbDataAdapter
'This declares the variable that will handle the data adapted that connects to the Database to VB
Dim sql As String
'A string that will handle SQL which tells the location of data
Private Sub BtnNewEmp_Click(sender As Object, e As EventArgs) Handles btnNewEmp.Click
Dim strProvider As String
Dim strSource As String
Dim strConnString As String
strProvider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
strSource = "../FuelAgencyDataBase.accdb"
strConnString = strProvider & strSource
dbConnection.ConnectionString = strConnString
dbConnection.Open()
sql = "SELECT * FROM EmployeeTable"
da = New OleDbDataAdapter(sql, dbConnection)
da.Fill(ds, "EmployeeTable")
dbConnection.Close()
For Each row In ds.Tables("EmployeeTable").Rows
Dim Test As String = (row.item(1)) & " " + (row.item(2)) & " - " & (row.item(6))
lbxHours.Items.Add(Test)
Next
Dim cb As New OleDbCommandBuilder(da)
'Variable handles how Visual Studio connects to the database
Dim dsNewRow As DataRow
'Variable handles how new rows are added to the record
dsNewRow = ds.Tables("EmployeeTable").NewRow()
dsNewRow.Item(1) = StrFirstName + " " + StrLastName
dsNewRow.Item(2) = StrUsername
dsNewRow.Item(3) = StrPassword
dsNewRow.Item(4) = StrProfession
ds.Tables("EmployeeTable").Rows.Add(dsNewRow)
da.Update(ds, "EmployeeTable")
MsgBox("Employee has been added into the database")
End Sub
我与数据库有实时连接,因为其他代码已连接到数据库。表名称为“EmployeeTable”。其他人可以找到解决此错误的方法吗?
【问题讨论】:
-
看不到 INSERT 语句。
-
@June7,命令生成器会自动生成。
-
查看以下关于此问题的参考资料:stackoverflow.com/questions/21880239/…
标签: vb.net visual-studio ms-access