VB6+SQL-Server:如何使用 ADODB.Command 执行带有命名参数的查询?

VB6+SQL-Server: How can I execute a Query with named parameters using ADODB.Command?(VB6+SQL-Server:如何使用 ADODB.Command 执行带有命名参数的查询?)
本文介绍了VB6+SQL-Server:如何使用 ADODB.Command 执行带有命名参数的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在尝试使用 ADODB.Command 执行参数化查询.我知道我可以使用?"对于参数,但我的查询相当大,我真的不想跟踪参数的确切顺序.我尝试了以下内容:

I've been trying to execute a parametrized query with ADODB.Command. I know I can use "?" for parameters, but my queries are rather large and I really don't want to track the exact order of the parameters. I tried something like the following:

objCmd.CommandType = adCmdText
objCmd.CommandText = "SELECT ... WHERE field1=@p_Field1 ...."    
Dim objParam As ADODB.Parameter
Set objParam = objCmd.CreateParameter("@p_Field1" ...)
objCmd.Parameters.Append objParam
...
objCmd.Open

它适用于存储过程参数(显然,设置 CommandType = adCmdStoredProc),但由于查询本身的动态特性,我无法在存储过程中执行此操作.当我尝试运行查询时,出现错误:

It works for stored procedure parameters (setting CommandType = adCmdStoredProc, obviously), but I can't do this inside a Stored Procedure because of the dynamic nature of the query itself. When I try to run the query, I get the error:

 Must declare the scalar variable "@p_Field1"

有没有其他不涉及使用存储过程或(argh)连接查询本身中的参数值而不使用参数的其他方法?

Is there any other way around this that doesn't involve using stored procedures or (argh) concatenating the parameters values in the query itself and not use parameters at all?

推荐答案

您可以这样做,以允许您重用参数

You can do this, to allow you to reuse parameters

objCmd.CommandText = "declare @var1 int, @var2 int, @var3 int; " & _
    "select @var1 = ?, @var2 = ?, @var3 = ?;" & _
    "select ... where field1 = @var1 or field4 = @var1 or field2 = @var2 ... "

并按正常顺序添加参数.

and add the parameters in the normal ordered manner.

这篇关于VB6+SQL-Server:如何使用 ADODB.Command 执行带有命名参数的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending parameters to stored procedures vb.net(将参数发送到存储过程 vb.net)
Insert multiple rows, count based on another table columns(插入多行,根据另一个表列计数)
How to hold the location of an image in a SQL Server database?(如何在 SQL Server 数据库中保存图像的位置?)
How to send to email (outlook) the selected items in SQL Server database using vb.net(如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook))
datagrid checkbox writes Null instead of 0 (false) into database(datagrid 复选框将 Null 而不是 0 (false) 写入数据库)
DBCC CheckDb-any ways to detect errors vb.net?(DBCC CheckDb-vb.net 有什么检测错误的方法吗?)