insert into语句中的语法错误 - 在VB.NET中

时间:2017-07-29 11:29:53

标签: vb.net oledbcommand

我正在尝试使用以下代码将记录插入MS Access数据库。我在项目中多次使用相同类型的代码。但我不知道为什么它是错误的,说有一个语法错误。有人请告诉我代码错误的地方。

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

2 个答案:

答案 0 :(得分:2)

大小是MS-Access的保留关键字。如果要将该单词用作列名,则应始终将其括在方括号

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

答案 1 :(得分:0)

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

如果出现此类错误,请始终使用方括号。

     

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

     

祝你好运