Solved

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

Posted on 2013-01-16
5
641 Views
Last Modified: 2013-01-17
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
0
Comment
Question by:iscivanomar
5 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 38784715
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
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 150 total points
ID: 38784723
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38784725
Looks like @AielloJ missed the

Visual Basic 6 no Visual Basic.NET
0
 
LVL 9

Assisted Solution

by:shorvath
shorvath earned 100 total points
ID: 38787347
Absolutely the easiest way to work with VB6 and Stored Procedures is the ADO Stored Procedure Add-IN from Microsoft.  This Add-In integrates with the VB6 IDE and all you have to do is select the server and stored procedure and it writes all of the code for you.  

Although you can't get it from Microsoft anymore, I did manage to find it for you at:

http://www.vbrad.com/article.aspx?id=103

Just click the download link and install as per instructions.

After you see how easy this is, you can then get a little bit more advanced.
This link shows you how to customize the add-in for you own purposes.  I remember creating server specific custom add-ins using this method and it saved me hundreds of hours of coding over the years.

http://msdn.microsoft.com/en-us/magazine/cc301907.aspx
0
 

Author Closing Comment

by:iscivanomar
ID: 38788984
Thank you for your help,
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question