VB 调用SQL SERVER 存储过程

2024-07-27 18:18:53
推荐回答(1个)
回答1:

'引用Microsoft ActiveX Data Objects 2.8 Library
Dim Cnn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Cnn_c As New ADODB.Command
Dim cnn_p As ADODB.Parameter
Set Cnn = New ADODB.Connection
Set Rs = New ADODB.Recordset
'On Error Resume Next
Cnn.ConnectionString = ConnStr '连接字芦念饥符串
With Cnn
.CursorLocation = adUseClient
.Open '连接
End With
Set Cnn_c.ActiveConnection = Cnn
With Cnn_c
.CommandType = adCmdStoredProc '命令类型:存储高帆过程
.CommandText = "AA" '存储过程陪返名
End With
Set cnn_p = Cnn_c.CreateParameter("ReTurn", adInteger, adParamReturnValue)
Cnn_c.Parameters.Append cnn_p '添加参数ReTurn(返回值)
Set Rs = Cnn_c.Execute '执行
MsgBox Rs.RecordCount '显示记录数
If Rs.RecordCount > 0 Then
Rs.Close
End If
Cnn.Close
Set cnn_p = Nothing
Set Cnn_c = Nothing
Set Rs = Nothing
Set Cnn = Nothing