Vb.net 我无法通过Visual Basic将新行保存到Microsoft Access数据库中
这段代码在我的程序的其他地方工作,但在这种形式下它不再工作。我是带着错误来的Vb.net 我无法通过Visual Basic将新行保存到Microsoft Access数据库中,vb.net,visual-studio,ms-access,Vb.net,Visual Studio,Ms Access,这段代码在我的程序的其他地方工作,但在这种形式下它不再工作。我是带着错误来的 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
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”。其他人能找到此错误的解决方案吗?这通常是因为您的列名之一是保留字或包含空格或其他特殊字符。使用命令生成器时,它只是将列名按原样复制到动作命令中。两个主要选项如下:
SELECT
语句中使用通配符。完整地写出列列表,然后需要转义任何无效的列名,命令生成器也会这样做QuotePrefix
和QuoteSuffix
属性,以便它将所有列名包装在这些字符中,从而转义需要它的列名。对于访问,值将分别为“[”
和“]”
看不到INSERT语句。@June7,命令生成器会自动生成该语句。请检查有关此问题的以下参考: