这是开发版中关于调用存储过程传递参数的例程,供大家参考
- Public Sub TestParameter
- ' 这段代码演示:
- ' 1. 创建一个使用输入/输出参数的存储过程
- ' 2. 创建参数并调用存储过程
- ' 3. 显示结果并删除这个存储过
- Dim conn As ADODB.Connection
- Dim Param As ADODB.Parameter
- Dim Com As ADODB.Command
- Dim strConn As String
- ' 给连接字符串变量赋值
- strConn = "PROVIDER=SQLOLEDB;SERVER=(local);User Id=sa;Password=;DATABASE=pubs"
- ' 建立连接对象
- Set conn = New ADODB.Connection
- ' 给连接对象赋予连接字符串并打开连接
- conn.ConnectionString = strConn
- conn.Open strConn
- ' 建立新的命令对象
- Set Com = New ADODB.Command
- ' 在PUBS数据库中创建一个新的存储过程
- Com.CommandText = "Create Procedure sp_ReturnsOutput @authorid " & _
- "varchar(11),@result Varchar(20) OUTPUT As Select @result " _
- & "= (Select au_fname from authors Where Au_id = @authorId)"
- Com.ActiveConnection = conn
- Com.Execute
- ' 现在已经创建两个参数: 一为输入,一为输出
- ' 在ADO对象中创建输入参数
- Set Param = Com.CreateParameter("AuId", adVarChar, adParamInput, 11)
- Param.Value = "172-32-1176"
- Com.Parameters.Append Param
- ' 在ADO对象中创建输出参数
- Set Param = Com.CreateParameter("Return", adVarChar, adParamReturnValue, 20)
- Com.Parameters.Append Param
- ' 注:当创建参数时,你必须包括参数的大小(长度)
- ' 加载命令对象的SQL字符串
- Com.CommandText = "sp_ReturnsOutput"
- Com.CommandType = adCmdStoredProc
- ' 调用
- Com.Execute
- ' 显示结果
- MsgBox "The first name of the author with an SS of " & _
- Com.Parameters(0).Value & " is " & Com.Parameters(1).Value
- ' 删除存储过程
- Com.CommandText = "Drop Procedure Sp_ReturnsOutput"
- Com.CommandType = adCmdText
- Com.Execute
- conn.Close
- End Sub
|