找出 SQLCommand 的 SQLParameters
在过去的某个时刻,在我的 VB6 时代,我记得能够创建一个 sql 命令对象(不一定与今天的 .NET 风格相同),并自动填充 sql 参数。
这允许我做一些事情就像只传递我明确知道存在的参数一样,如果两个不同的客户端使用不同版本的数据库,我可以调用我的过程,因为我知道我仍然会得到有意义的响应。
像这样的事情:
Dim cmd as SqlCommand
Set cmd = New SqlCommand(Connection)
cmd.CommandText = "MagicStoredProcedure"
cmd.CommandType = CommandType.StoredProcedure
If cmd.Parameters.Count > 0 Then
If cmd.Parameters(0).Name = "@FirstParameter" Then
cmd.Parameters("@FirstParameter").Value = someValue
End If
End If
Dim r as Recordset
Set r = cmd.ExecuteRecordset()
我记得这样做过,但我找不到自己的任何示例,并且尝试在 .NET 中这样做似乎根本不起作用。我见过的所有示例(并且我已经搜索了一段时间)手动添加参数。
有什么指点吗?
At some point in the past, in my VB6 days, I remember being able to create a sql command object (not necessarily the same one as today's .NET flavour), and have the sql parameters automatically filled in.
This allowed me do do things like only passing in parameters that I definitely knew to exist, and if two different clients were using different versions of the database, I could call my procedures knowing that I would still get a meaningful response.
Something like this:
Dim cmd as SqlCommand
Set cmd = New SqlCommand(Connection)
cmd.CommandText = "MagicStoredProcedure"
cmd.CommandType = CommandType.StoredProcedure
If cmd.Parameters.Count > 0 Then
If cmd.Parameters(0).Name = "@FirstParameter" Then
cmd.Parameters("@FirstParameter").Value = someValue
End If
End If
Dim r as Recordset
Set r = cmd.ExecuteRecordset()
I remember doing this, but I cannot find any examples of my own, and trying to do this in .NET does not seem to work at all. All the examples I have seen (and I have searched for some time) add the parameters manually.
Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了 SQLCommandBuilder.DeriveParameters 过程,它正是我想要的。
但不是我所期望的。我希望将其融入到 Command 对象或参数集合中。
I have found the SQLCommandBuilder.DeriveParameters procedure, which does exactly what I wanted.
Not where I expected it, though. I'd expect that baked in to the Command object, or in the Parameters collection.
我过去曾使用一个模板解决方案进行数据库访问,它使用映射到存储过程变量的类属性,并在需要时动态构建字符串。
查看此处的说明
它看起来与您得到的非常相似当您使用 Visual Studio 将存储过程和表从连接的数据库添加到项目时。
也许它可以为您指明正确的方向。
There's a template solution that I have used in the past for DB access, that uses a class's properties mapped to stored procedure variables and builds the string on the fly when needed.
Look here for an explanation
It looks very similar to what you get when you add stored procedures and tables from a connected database to a project when using Visual Studio.
Perhaps it can point you in the right direction.