2012-02-06 75 views
0

目前的進度邁向SOLUTION:插入命令OLE參數

這是更新的代碼:

sql = "INSERT INTO Strings (String_Name, Long_Text, Short_Text, Alternate_Text, Multi_String_ID, Lang_ID) VALUES (?,?, ?,?,?,?)" 

Dim command = New OleDbCommand(sql, pConn) 

command.Parameters.AddWithValue("@webStringName", "String_Name") 
command.Parameters.AddWithValue("@webLang_String", "Long_Text") 
command.Parameters.AddWithValue("@ptsShortText", "Short_Text") 
command.Parameters.AddWithValue("@webAltText", "Alternate_Text") 
command.Parameters.AddWithValue("@webMultiStringID", "Multi_String_ID") 

改變上述行來這... command.Parameters.AddWithValue( 「@ webMultiStringID」,OleDbType .Integer)。價值= webMultiStringID

command.Parameters.AddWithValue("@webLang_Code", "Lang_ID") 

command.ExecuteNonQuery() 

ORIG POST:

我正在嘗試使用和OLE適配器創建和INSERT語句。我沒有paramtersm工作,但現在我正在嘗試添加parms,並且遇到了語法問題。哈

這裏是到目前爲止的代碼...

command = New OleDbCommand(sql, pConn) 

sql = "INSERT INTO Strings (String_Name, Long_Text, Short_Text) VALUES (?,?, """ & ptsShortText & """)" 


command.Parameters.Add("@webStringName", OleDbType.VarChar, 250, "String_Name") 
command.Parameters.Add("@webLang_String", OleDbType.VarChar, 250, "Long_Text") 

command = New OleDbCommand(sql, pConn) 
command.ExecuteNonQuery() 

只是試圖讓前兩個變量參數。

任何建議將不勝感激。

編輯:固定SQL

+0

爲什麼不走一路和參數所有三個參數... – 2012-02-06 17:50:03

回答

2

你做錯了什麼。你先添加參數:

command.Parameters.Add("@webLang_String", OleDbType.VarChar, 250, "Long_Text") 

然後有一個新的(不帶參數)取代命令:

command = New OleDbCommand(sql, pConn) 

因此,您刪除該行的所有現有的參數。這就是爲什麼它不起作用。


你必須這樣做以正確的順序:

sql = "INSERT INTO Strings (String_Name, Long_Text, Short_Text) " & _ 
     "VALUES (?,?, """ & ptsShortText & """)" 

Dim command = New OleDbCommand(sql, pConn) ' First create the command 

' Then add the parameters to the command 
command.Parameters.AddWithValue("@webStringName", "String_Name") 
command.Parameters.AddWithValue("@webLang_String", "Long_Text") 

' Then execute the command. (DON'T recreate it with New OleDbCommand here, 
' that would throw away all you've done so far.) 
command.ExecuteNonQuery() 
+0

不知道我理解,你的代碼是什麼我有。 – htm11h 2012-02-06 17:45:43

+0

@ marc11h:是的,我試圖告訴你你做錯了什麼,*爲什麼*它不起作用。 ;-)我已經擴展了我的答案,向您展示了一個可行的解決方案。 – Heinzi 2012-02-06 17:49:39

+0

我現在只收到一個錯誤消息:標準表達式中的數據類型不匹配。列表中只有一個整數。我會更新我的原始代碼。 – htm11h 2012-02-06 18:01:52