2012-02-06 78 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