Link to home
Start Free TrialLog in
Avatar of iscivanomar
iscivanomarFlag for United States of America

asked on

How to execute a SQL Server 2005 Stored Procedure and get a return value in VB6?

Hello everyone,
                    I have an application in Visual Basic 6 and I need to execute a stored procedure and get a return value.  How can I do this? what is the code that I can use.

Visual Basic 6 no Visual Basic.NET

Thank you
Avatar of AielloJ
AielloJ
Flag of United States of America image

iscivanomar,

It's been awhile, but this should work for you:

CREATE PROCEDURE my_proc
            @param1 varchar(10),
            @param2 int
:
:
:
:
Public Shared Function my_function(ByVal myVB_Param As String) As String

    'create connection
    Using cnSQL As SqlConnection = New SqlConnection("MyConnectionString")
        Using cmdSP As New SqlCommand("my_proc", cnSQL)
            cmdSP.CommandType = System.Data.CommandType.StoredProcedure

            'declare and add  parameter
            cmdSP.Parameters.Add("@param1", System.Data.SqlDbType.NVarChar, 50).Value = "ABC"
            cmdSP.Parameters.Add("@param2", System.Data.SqlDbType.Integer, 0).Value = 0

            Dim dtTable As New DataTable

            'execute command
            Try
                cnSQL.Open()
                dtTable.Load(cmdSP.ExecuteReader())
                cnSQL.Close()
            Catch ex As SqlException
                'do something here
            End Try

            'return string
            Return dtTable.Rows(0)(0)
        End Using
    End Using

End Function

Best regards,

AielloJ
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks like @AielloJ missed the

Visual Basic 6 no Visual Basic.NET
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iscivanomar

ASKER

Thank you for your help,