home


Call stored procedure from visual basic
Author Nigel Rivett

This is a simple stored procedure call for testing.
See the database access layer for a better solution

Dim sServer As String
Dim sUser As String
Dim sPWD As String
Dim sDatabase As String

Dim DBcon As New ADODB.Connection
Dim objCmd As New ADODB.Command
Dim objparameter As New ADODB.Parameter
Dim objRs As New ADODB.Recordset
    
    sServer = "(local)"
    sDatabase = "mydb"
    sUser = "sa"
    sPWD = "password"
    
    DBcon.ConnectionString = "Provider=sqloledb;" & _
        "server=" & sServer & ";uid=" & sUser & ";pwd=" & sPWD & ";database=" & sDatabase
    DBcon.CursorLocation = adUseClient
    DBcon.Open
    
    objparameter.Direction = adParamInput
    objparameter.Type = adVarChar
    objparameter.Size = 5
    objparameter.Value = "hello"
    objCmd.Parameters.Append objparameter
    
    objCmd.ActiveConnection = DBcon
    objCmd.CommandType = adCmdStoredProc
    objCmd.CommandText = "s_mysp"
    Set objRs = objCmd.Execute                       ' objCmd.Execute for no resultset
   
    set objRs.ActiveConnection = Nothing
    Set objCmd = Nothing
    DBcon.Close



home