来自代码的参数化查询 [英] parameterized query from code

查看:76
本文介绍了来自代码的参数化查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以看到失败的地方吗?


Private Sub Button2_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理Button2。点击


da.SelectCommand.Parameters.Add(" @ CustomerID",

SqlDbType.VarChar,40)

Dim ds作为新数据集

da.SelectCommand.Parameters(" @ CustomerID")。Value = myCustID

da.Fill(ds," Customers")

End Sub


我在填充命令中遇到的错误是:变量名称

''@ CustomerID''已经宣布。变量名称在查询批处理或存储过程中必须是唯一的




被引用的数据适配器(da)位于带有
selectcommand在查询构建器中设置如下:

SELECT OrderID,CustomerID,OrderDate,EmployeeID

来自订单

WHERE CustomerID = @CustomerID


我似乎无法正确地将此值传递给查询。我已经尝试了

从代码中取出@符号,但完全相同的错误。


谢谢,Ken

Can anybody see where this is failing?

Private Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click

da.SelectCommand.Parameters.Add("@CustomerID",
SqlDbType.VarChar, 40)
Dim ds As New DataSet
da.SelectCommand.Parameters("@CustomerID").Value = myCustID
da.Fill(ds, "Customers")
End Sub

The error that I am getting on the fill command is: "The variable name
''@CustomerID'' has already been declared. Variable names must be unique
within a query batch or stored procedure."

The dataadapter being referenced (da) is on the form with a
selectcommand set up in the query builder as such:

SELECT OrderID, CustomerID, OrderDate, EmployeeID
FROM Orders
WHERE CustomerID = @CustomerID

I can''t seem to pass this value into the query correctly. I''ve tried
taking the @ signs out of the code, but exact same error.

thanks, Ken

推荐答案

" Ken" < fk **** @ yahoo.comwrote in message

news:11 ********************** @ y80g2000hsf.googlegr oups.com ...
"Ken" <fk****@yahoo.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...

任何人都可以看到失败的地方吗?


Private Sub Button2_Click(ByVal sender As Object ,ByVal e As

System.EventArgs)处理Button2.Click


da.SelectCommand.Parameters.Add(" @ CustomerID",

SqlDbType.VarChar,40)

Dim ds作为新数据集

da.SelectCommand.Parameters(" @ CustomerID")。Value = myCustID

da.Fill(ds," Customers")

End Sub


我在填充命令中遇到的错误是: 变量名称

''@ CustomerID''已经被声明。变量名称在查询批处理或存储过程中必须是唯一的

Can anybody see where this is failing?

Private Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click

da.SelectCommand.Parameters.Add("@CustomerID",
SqlDbType.VarChar, 40)
Dim ds As New DataSet
da.SelectCommand.Parameters("@CustomerID").Value = myCustID
da.Fill(ds, "Customers")
End Sub

The error that I am getting on the fill command is: "The variable name
''@CustomerID'' has already been declared. Variable names must be unique
within a query batch or stored procedure."



每次点击Button2时你都会添加一个新参数!可能它

已经有参数取决于它的设置方式。一般来说,我只需要在代码中创建这些东西,而不是将它们放在表单上,​​但是假设

它可以在设计时添加参数。


Michael

You''re adding a new parameter every time you click Button2! Possibly it
already has the parameter depending on how it is setup. Generally I just
create these things in code instead of putting them on the form but presume
it''s possible to add params at design time.

Michael


>
>

每次点击Button2时你都会添加一个新参数!可能它

已经有参数取决于它的设置方式。一般来说,我只需要在代码中创建这些东西,而不是将它们放在表单上,​​但是假设

它可以在设计时添加参数。
< br $>
Michael
You''re adding a new parameter every time you click Button2! Possibly it
already has the parameter depending on how it is setup. Generally I just
create these things in code instead of putting them on the form but presume
it''s possible to add params at design time.

Michael



这就是我所害怕的。我很乐意在代码中完成整个过程,

但是我正在使用VB Express,并且它不会将新的

SqlDataAdapter识别为有效的命令。这就是为什么我把适配器放在

表格上的原因。知道如何将参数值从

传递到查询中吗?


非常感谢您的回复,

Ken

That''s what I was afraid of. I''d love to do the whole thing in code,
but I''m using VB Express, and it doesn''t recognize the New
SqlDataAdapter as a valid command. That''s why I put the adapter on
the form instead. Any idea how I can pass the parameter value from
the code into the query?

Thanks alot for the response,
Ken



>

您要添加一个新参数每次单击Button2!
>
You''re adding a new parameter every time you click Button2!



这就是我所害怕的。

That''s what I was afraid of.


可能它已经有参数取决于它是如何设置的。一般来说,我只需要在代码中创建这些东西,而不是将它们放在表单上,​​但是假设

它可以在设计时添加参数。
Possibly it already has the parameter depending on how it is setup. Generally I just
create these things in code instead of putting them on the form but presume
it''s possible to add params at design time.



我很乐意在代码中执行此操作,但我使用的是VB Express,并且它没有b $ b将New SqlDataAdapter识别为有效的命令。这就是我将

适配器放在表单上的原因。有关如何将

参数值传递给上面构造的查询的任何想法?或者有更好的方法吗?该值最初来自表格上的文本框




感谢您的帮助,Michael

Ken


I''d love to do this in code, but I''m using VB Express, and it doesn''t
recognize New SqlDataAdapter as a valid command. That''s why I put the
adapter on the form instead. Any idea as to how I can pass the
parameter value into the query as structured above? Or is there a
better way to do this? The value is originally coming from a textbox
on the form.

Thanks for your help, Michael
Ken



这篇关于来自代码的参数化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆