2013-10-17 49 views
0

我DBStructure如下:通過vb.net OLEDB插入日期時間,查詢提供了錯誤

enter image description here

我想它插入一條記錄,所以我燒以下查詢:

insert into LocalBarcode (PescaLocation,Barcode,TimeStamp,IsUpload)values('1','11','10/17/2013 

6:08:57 PM') 

但它給我的語法錯誤:如

插入到的語法錯誤。

對於IsUpload,我已將默認值保留爲'N',因此我不會從這裏發送它。

LBID是自動增量。

請幫忙。

VB.NET查詢:

Dim sqlInsertBarcode = "insert into LocalBarcode 

(PescaLocation,Barcode,TimeStamp,IsUpload)values('" & pescaLocation & "','" & 

txtBarcode.Text.Trim.Replace("'", "''") & "','" & Now() & "') " 
+1

在.net代碼中,使用查詢參數和適當的數據類型。 '10/17/2013'是一個字符串,不是日期。 –

回答

2

您也可以嘗試更多的東西是這樣的:

Try 
     dbConnection.Open() 
     Dim cmd As New OleDbCommand 
     cmd.CommandText = "INSERT INTO LocalBarcode (PescaLocation, Barcode, TimeStamp, " & _ 
          "IsUpload) VALUES " & _ 
          "(@pescalocation, @barcode, @whatever, @whatever)" 
     cmd.Parameters.AddWithValue("@Pescalocation", pescalocation) 
     cmd.Parameters.AddWithValue("@Barcode", txtBarcode.text) 
     cmd.Parameters.AddWithValue("@TimeStamp", whatever) 
     cmd.Parameters.AddWithValue("@IsUpload", whatever) 
     cmd.ExecuteNonQuery() 
    Catch ex As Exception 
    Finally 
     dbConnection.Close() 
    End Try 

我會繼續前進,使變量的 「現在」 和 「IsUpload」以便您可以更輕鬆地使用AddWithValue。