插入到语句中的语法错误 - 在VB.NET中

问题描述:

我试图用下面的代码将记录插入到MS Access数据库中。我在我的项目中多次使用了相同类型的代码。但我不知道它为什么错误,说有一个语法错误。有人请告诉我代码出错的地方。插入到语句中的语法错误 - 在VB.NET中

Try 
       If MainForm.con.State = ConnectionState.Closed Then 
        MainForm.con.Open() 
       End If 
       Dim cmdText As String 
       cmdText = "insert into tblBottling(bottlingDate,workerName,seed,size,noOfBottles,timeTaken,remarks) values(?,?,?,?,?,?,?)" 
       Dim command As OleDbCommand = New OleDbCommand(cmdText, MainForm.con) 

       command.Parameters.AddWithValue("@bottlingDate", botDate.Value.ToString("dd-MM-yy")) 
       command.Parameters.AddWithValue("@workerName", workerCB.SelectedItem.ToString) 
       command.Parameters.AddWithValue("@seed", seedCB.SelectedItem.ToString) 
       command.Parameters.AddWithValue("@size", botSizeCB.SelectedItem.ToString) 
       command.Parameters.AddWithValue("@noOfBottles", CInt(noOfBot.Text)) 
       command.Parameters.AddWithValue("@timeTaken", timeTakenTxt.Text) 
       command.Parameters.AddWithValue("@remarks", remarksTxt.Text) 

       command.ExecuteNonQuery() 
       MainForm.con.Close() 

      Catch ex As Exception 
       MsgBox(ex.ToString) 
      End Try 

尺寸为MS-访问保留关键字。如果你想使用这个词作为列名,那么你应该总是包围方括号

cmdText = "insert into tblBottling 
      (bottlingDate,workerName,seed,[size],noOfBottles,timeTaken,remarks) 
      values(?,?,?,?,?,?,?)" 
+0

由于一吨!对此很迷惑...... – sabari

`cmdText = "insert into tblBottling([bottlingDate],[workerName],[seed],[size],[noOfBottles],[timeTaken],[remarks]) values(?,?,?,?,?,?,?)" 

之间请永远使用方括号为您列柜面这样的错误。

因为你无法记住所有的关键字。

好运