2017-07-29 81 views
0

我試圖用下面的代碼將記錄插入到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 

回答

2

尺寸爲MS-訪問保留關鍵字。如果你想使用這個詞作爲列名,那麼你應該總是包圍方括號

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

由於一噸!對此很迷惑...... – sabari

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

之間請永遠使用方括號爲您列櫃面這樣的錯誤。

因爲你無法記住所有的關鍵字。

好運