2

我无法将句柄值传递给 sp_execute。如果我直接在 Management Studio 中使用 SQL 代码,我会成功:

Declare @handle Integer
exec sp_prepare @handle OUTPUT, N'@year int', N'SELECT * from Sales.CreditCard where ExpYear=@year';

select @handle

exec sp_execute @handle, 2007
exec sp_unprepare @handle

但是当我在 VB 上尝试以下代码时,执行命令后“param3.Value”为“nothing”。

    ConnString = "DRIVER={SQL Server};SERVER=192.168.100.41,1433;UID=sa;PWD=<password>;DATABASE=AdventureWorks"
    'create an instance of the ADO connection
    Connection = CreateObject("ADODB.Connection")

    'Open the connection to the database
    Connection.Open(ConnString)
    Dim cmd As ADODB.Command
    Dim param1 As ADODB.Parameter
    Dim param2 As ADODB.Parameter
    Dim param3 As ADODB.Parameter

    cmd = CreateObject("ADODB.Command")
    cmd.ActiveConnection = Connection
    cmd.CommandText = "sp_prepare"

    param1 = cmd.CreateParameter("@handle", 3, 2, 4, 0)
    param2 = cmd.CreateParameter("@params", 202, 1, 200, "@year Integer")
    param3 = cmd.CreateParameter("stmt", 202, 1, 200, "SELECT TOP 4 [CreditCardID],[CardType],[CardNumber],[ModifiedDate]  FROM [AdventureWorks].[Sales].[CreditCard] where [ExpYear] in (@year)")

    cmd.Parameters.Append(param1)
    cmd.Parameters.Append(param2)
    cmd.Parameters.Append(param3)

    cmd.Execute()

    Debug.WriteLine("handle:" & param1.Value)

你知道如何让它在VB上工作吗?我从 VB 运行它的原因是我需要通过 RPC 运行该命令。

4

1 回答 1

0

尝试改变:

 param1 = cmd.CreateParameter("@handle", 3, 2, 4, 0)

param1 = cmd.CreateParameter("@handle", 3, 4, 4, 0)

那是变化ParameterDirection。如果您注意到sp_prepare评估handle但不显示,除非您调用select @handle

于 2012-11-01T07:19:41.300 回答