插入命令 OLE 参数化

发布于 2025-01-02 23:02:44 字数 1377 浏览 3 评论 0原文

解决方案的当前进展:

这是更新后的代码:

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).Value = webMultiStringID

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

command.ExecuteNonQuery()

ORIG POST:

我正在尝试使用 OLE 适配器创建和 INSERT 语句。我让它在没有参数的情况下工作,但现在我尝试添加参数并且遇到语法问题。哈

这是到目前为止的代码...

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

CURRENT PROGRESS TOWARD SOLUTION:

This is the updated code:

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")

Changed above line to this...
command.Parameters.AddWithValue("@webMultiStringID", OleDbType.Integer).Value = webMultiStringID

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

command.ExecuteNonQuery()

ORIG POST:

I am trying to create and INSERT statement with and OLE adapter. I had it working without paramtersm but now I am trying to add parms and am having issues with the syntax. Ha

Here is the code so far...

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()

Was just trying to get the first two variables parameterized.

Any suggestions would be greatly appreciated.

EDIT: Fixed SQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2025-01-09 23:02:44

你做错了什么。首先添加参数:

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()

You are doing something wrong. You first add the parameters:

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

and then replace the command with a new one (with no parameters):

command = New OleDbCommand(sql, pConn)

Thus, you remove all your existing parameters in this line. That's why it doesn't work.


You have to do it in the correct order:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文