Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-16
5
Medium Priority
?
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 600 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 400 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
Suggested Courses

610 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