Advertisement

05.06.2005 at 02:40PM PDT, ID: 21415723
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Executing SQL Server stored procedure using VB.NET
Tags: procedure, stored, sql
Hi ,

I am pretty new to this.

I have a SQL Server stored (called SP_OESS_Get_Client_Demographics)  procedure that accepts following inputs:
@clientid varchar(10),
@firstname varchar(20),
@lastname varchar(20),
@middlename varchar(10),
@SSN varchar(11),
@Birthdate smalldatetime,
@Gender varchar(1)

and returns an XML result set. I want to know how I can execute this stored procedure using VB.NET. How to do I code it in VB.NET??

Thanks!

-Kunal
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: Kunal_Ghate
Solution Provided By: ptakja
Participating Experts: 3
Solution Grade: A
Views: 808
Translate:
Loading Advertisement...
05.06.2005 at 07:54PM PDT, ID: 13949209

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2005 at 08:28AM PDT, ID: 13954495

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2005 at 08:59AM PDT, ID: 13954607

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2005 at 09:06AM PDT, ID: 13954629

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.30.2005 at 01:20AM PDT, ID: 14107057

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.21.2005 at 06:50PM PDT, ID: 14272081

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.25.2005 at 12:05PM PDT, ID: 14301283

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.29.2005 at 01:52AM PDT, ID: 14326478

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.06.2005 at 07:54PM PDT, ID: 13949209

Rank: Wizard

Pretty straight forward. Here you go:

Here's a couple routines I use for this sort of thing:

    '****************************************************************************************
    ' ExecQuery
    ' ABSTRACT: Executes a stored procedure against the Eisemann database and returns
    '   a NEW Dataset with the selected data.
    '
    ' INPUT PARMS:  ProcedureName   Name of Stored Procedure to execute
    '               Parms           Array of SqlParameter objects that will be passed into the
    '                               stored procedure.
    '
    ' RETURNS:      DataSet populated with results from stored procedure execution.
    '
    '****************************************************************************************
    Private Overloads Function ExecQuery(ByVal ProcedureName As String, ByVal Parms As SqlParameter()) As DataSet

        Dim dsDataSet As New DataSet
        ' Configure the SqlCommand object
        With _cmdSqlCommand
            .CommandType = CommandType.StoredProcedure      'Set type to StoredProcedure
            .CommandText = ProcedureName                    'Specify stored procedure to run

            ' Clear any previous parameters from the Command object
            Call .Parameters.Clear()

            ' Loop through parmameter collection adding parameters to the command object
            If Not (Parms Is Nothing) Then
                For Each sqlParm As SqlParameter In Parms
                    _cmdSqlCommand.Parameters.Add(sqlParm)
                Next
            End If
        End With

        ' Configure Adapter to use newly created command object and fill the dataset.
        _adpAdapter.SelectCommand = _cmdSqlCommand
        _adpAdapter.Fill(dsDataSet)

        Return dsDataSet
    End Function

    '****************************************************************************************
    ' ExecNonQuery
    ' ABSTRACT: Executeds a non-query without any parameters.
    '
    ' INPUT PARMS: ProcedureName    stored procedure to execute.
    '
    ' RETURNS:  Integer indicating how many rows were affected by the non-query.
    '
    ' Copyright © 2005 by Corning, Inc.
    '****************************************************************************************
    Private Overloads Function ExecNonQuery(ByVal ProcedureName As String) As Integer
        Return ExecNonQuery(ProcedureName, Nothing)
    End Function


    '****************************************************************************************
    ' ExecNonQuery
    ' ABSTRACT: Executes a non-query stored procedure agains the Eisemann database.
    '
    ' INPUT PARMS:  ProcedureName   Name of stored procedure to execute
    '               Parms           Collection of SqlParameter objects used as arguments for
    '                               the stored procedure.
    '
    ' RETURNS:      An integer containing the number of rows affected by the Stored Procedure.
    '
    ' Copyright © 2005 by Corning, Inc.
    '****************************************************************************************
    Private Overloads Function ExecNonQuery(ByVal ProcedureName As String, ByVal Parms As SqlParameter()) As Integer

        Dim intRowsAffected As Integer

        ' Configure the _cmdSqlCommand object
        With _cmdSqlCommand
            .CommandType = CommandType.StoredProcedure  'Set type to Stored Procedure  
            .CommandText = ProcedureName                'Specify procedure to run

            ' Clear any previous parameters from the command object
            Call .Parameters.Clear()

            ' Loop through parmameter collection, if defined, adding parameters to the command object
            If Not (Parms Is Nothing) Then
                For Each sqlParm As SqlParameter In Parms
                    _cmdSqlCommand.Parameters.Add(sqlParm)
                Next
            End If
        End With
        If _cnnMyConnection.State <> ConnectionState.Open Then
            Call _cnnMyConnection.Open()
        End If
        ' Execute the procedure
        intRowsAffected = _cmdSqlCommand.ExecuteNonQuery()

        Return intRowsAffected                          'Return the number of rows affected by procedure

    End Function

To use these guys you create a sub like this:
Public Function GetDemographics(ClientID as String, FirstName as string, LastName as string, MiddleName as string, SSN as string, Bday as Date, Gender as string) As DataTable

        Dim MyDataSet as DataSet
        Dim Parms(6) As SqlParameter

        Parms(0) = New SqlParameter("@clientid", ClientID)
        Parms(1) = New SqlParameter("@firstname", FirstName)
        Parms(2) = New SqlParameter("@lastname", LastName)
        Parms(3) = New SqlParameter("@middlename", MiddleName)
        Parms(4) = New SqlParameter("@SSN", SSN)
        Parms(5) = New SqlParameter("@Birthdate", Bday)
        Parms(6) = New SqlParameter("@Gender", Gender)

        Return ExecuteQuery("SP_OESS_Get_Client_Demographics", Parms)
End function


You would need to create your SqlConnection object and a SqlDataAdapter object named _adpAdapter in order for this code to work. But this should get you started.
Accepted Solution
 
05.08.2005 at 08:28AM PDT, ID: 13954495
use this as your starting point:

Imports System.Xml
Imports System.IO
Imports System.Data.SqlClient
Imports System.Text

Public NotInheritable Class XmlReaderAdapter

    Private Sub New()

    End Sub

    Public Shared Function GetString(ByVal command As SqlCommand, Optional ByVal rootName As String = "root") As String
        Return GetString(command, Encoding.UTF8, rootName)
    End Function

    Public Shared Function GetString(ByVal command As SqlCommand, ByVal encoding As System.Text.Encoding, Optional ByVal rootName As String = "root") As String
        Dim memory As New MemoryStream
        Dim dataReader As XmlReader
        Dim writer As New XmlTextWriter(memory, encoding)
        Dim reader As New StreamReader(memory)
        writer.WriteStartDocument(True)
        writer.WriteStartElement(rootName)
        Try
            command.Connection.Open()
            dataReader = command.ExecuteXmlReader()
            dataReader.Read()
            While dataReader.Read
                writer.WriteRaw(dataReader.ReadOuterXml())
            End While
           
        Catch ex As Exception

        Finally
            If Not dataReader Is Nothing Then
                dataReader.Close()
            End If
        End Try
        writer.WriteEndDocument()
        writer.Flush()
        memory.Position = 0
        Dim value As String = reader.ReadToEnd()
        writer.Close()
        reader.Close()
        Return value
    End Function


End Class
Assisted Solution
 
05.08.2005 at 08:59AM PDT, ID: 13954607
this is an example of the class to call the XmlReaderAdapter

Imports System.Data.SqlClient
Imports System.Data

Public NotInheritable Class ClientDemographics


    Private Shared Function GetSqlCommand(ByVal connection As SqlConnection) As SqlCommand
        Dim command As SqlCommand = connection.CreateCommand()
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "SP_OESS_Get_Client_Demographics"
        With command.Parameters
            .Add("@RETURN_VALUE", SqlDbType.Int, 4)
            command.Parameters(0).Direction = ParameterDirection.ReturnValue
            .Add("@clientid", SqlDbType.VarChar, 20)
            .Add("@firstname", SqlDbType.VarChar, 20)
            .Add("@lastname", SqlDbType.VarChar, 20)
            .Add("@middlename", SqlDbType.VarChar, 10)
            .Add("@SSN", SqlDbType.VarChar, 11)
            .Add("@Birthdate", SqlDbType.SmallDateTime, 4)
            .Add("@Gender", SqlDbType.VarChar, 1)
        End With
        Return command
    End Function

    Public Sub New()
        MyBase.New()
    End Sub

    Public ID As String
    Public FirstName As String
    Public LastName As String
    Public MiddleName As String
    Public SSN As String
    Public BirthDate As DateTime
    Public Gender As String

    Public Function GetXmlString(ByVal connection As SqlConnection) As String
        Return XmlReaderAdapter.GetString(GetSqlCommand(connection))
    End Function

    Private Shared Function GetValue(ByVal item As DateTime) As Object
        Dim value As Object = DBNull.Value
        If DateTime.Compare(item, DateTime.MinValue) > 0 Then
            value = item
        End If
        Return value
    End Function

    Private Shared Function GetValue(ByVal item As String) As Object
        Dim value As Object = DBNull.Value
        If Not item Is Nothing AndAlso item.Length > 0 Then
            value = item
        End If
        Return value
    End Function





End Class



 
05.08.2005 at 09:06AM PDT, ID: 13954629
expanding the GetXmlString Function


Public Function GetXmlString(ByVal connection As SqlConnection) As String
        Dim command As SqlCommand = GetSqlCommand(connection)
        command.Parameters("@clientid").Value = GetValue(ID)
        command.Parameters("@firstname").Value = GetValue(FirstName)
        command.Parameters("@lastname").Value = GetValue(LastName)
        command.Parameters("@middlename").Value = GetValue(MiddleName)
        command.Parameters("@SSN").Value = GetValue(SSN)
        command.Parameters("@Birthdate").Value = GetValue(BirthDate)
        command.Parameters("@Gender").Value = GetValue(Gender)
        Dim value As String = XmlReaderAdapter.GetString(command)
        command.Dispose()
        Return value
End Function


Thus, you call the class like so:
Dim connection As New SqlConnection(...)
Dim demographics As New ClientDemographics
demographics.ID = ""
...
Dim value As String = demographics.GetXmlString(connection)

 
05.30.2005 at 01:20AM PDT, ID: 14107057
try this...kinda neat..
but you get the logics of how it flows/works...den put it on ur sides

---------------------------------------------------------------------------------
Dim cn As New SqlClient.SqlConnection()
Dim cm As New SqlClient.SqlCommand()

        With cn
            .ConnectionString = "user id=sa;password=;" & _
            "data source=localhost;" & _
            "initial catalog=database_name"
            .Open()
        End With

        With cm
            .CommandText = "stored_procedure"
            .CommandType = CommandType.StoredProcedure
            .Connection = cn
            'output parameters which you wanna shoot to the form from database
            .Parameters.Add("@getout2", SqlDbType.Int)
            .Parameters("@getout2").Direction = ParameterDirection.Output
            'input parameters for condition purpose taken from textbox.text
            .Parameters.Add("@month", textbox.Text)
        End With

        Try
            cm.ExecuteNonQuery()
        Catch exp As SqlClient.SqlException
            MsgBox(exp.Message)
        End Try

        'display output parameter based on input parameter into label1.text
      Label1.Text = cm.Parameters("@getout2").Value
---------------------------------------------------------------------------------

hope it helps....
do let me know if it works...
Assisted Solution
 
06.21.2005 at 06:50PM PDT, ID: 14272081

Rank: Genius

Kunal_Ghate,
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in approximately four days.  I would appreciate any comments by the experts that would help me in making a recommendation.

It is assumed that any participant not responding to this request is no longer interested in its final disposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help.jsp#hs5

TheLearnedOne
EE Cleanup Volunteer
 
06.25.2005 at 12:05PM PDT, ID: 14301283

Rank: Genius

I will leave the following recommendation for this question in the Cleanup topic area:
   Split: ptakja {http:#13949209} & b1xml2 {http:#13954495} & nwhan {http:#14107057}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

TheLearnedOne
EE Cleanup Volunteer
 
06.29.2005 at 01:52AM PDT, ID: 14326478
Forced accept.

modulo
Community Support Moderator
 
 
20080236-EE-VQP-29