?
Solved

run stored procedure from vb.net

Posted on 2016-08-15
4
Medium Priority
?
71 Views
Last Modified: 2016-08-15
I would like to run the stored procedure but I dont need any data from it as its doing an update and some housekeeping.

how do i do that without having to create a reader etc.

normally i would do this:

   dtWords = New DataTable
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString

        Using conn As New SqlConnection(connectionString)
            Using cmd As New SqlCommand(StrProcName, conn)
                cmd.CommandTimeout = 0
                cmd.CommandType = CommandType.StoredProcedure
                conn.Open()

                cmd.Parameters.AddWithValue("@Client", StrClient)

                If OpShortDescCars.Checked = True Or OpShortDescBikes.Checked = True Or OpShortDescLcv.Checked = True Or OpShortDescOthers.Checked = True Then
                    cmd.Parameters.AddWithValue("@VCategory", StrVehCat)

                ElseIf OpShortDescCarsLcvOthers.Checked = True Or OpShortDescBikesQuads.Checked = True Then
                    cmd.Parameters.AddWithValue("@VCatSuper", StrVehCat)

                ElseIf OpShortDescAll.Checked = True Then
                    ' no params
                End If

                Dim reader As SqlDataReader = cmd.ExecuteReader()
                dtWords.Load(reader)
            End Using
        End Using
        Return dtWords

Open in new window

0
Comment
Question by:PeterBaileyUk
[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
  • 3
4 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 total points
ID: 41757393
try use ExecuteScalar? like:

cmd.CommandTimeout = 0
                cmd.CommandType = CommandType.StoredProcedure
                conn.Open()

                '....

cmd.ExecuteScalar()

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41757408
Will this do the job
    Public Sub CreateAllStrs()

        Dim StrProcName As String
        StrProcName = "usp_CreateStrStringsPlusTidyup"

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString

        Using conn As New SqlConnection(connectionString)
            Using cmd As New SqlCommand(StrProcName, conn)
                cmd.CommandTimeout = 0
                cmd.CommandType = CommandType.StoredProcedure
                conn.Open()
                cmd.ExecuteReader()
            End Using
        End Using

    End Sub

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41757409
so to this
    Public Sub CreateAllStrs()

        Dim StrProcName As String
        StrProcName = "usp_CreateStrStringsPlusTidyup"

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString

        Using conn As New SqlConnection(connectionString)
            Using cmd As New SqlCommand(StrProcName, conn)
                cmd.CommandTimeout = 0
                cmd.CommandType = CommandType.StoredProcedure
                conn.Open()
                cmd.ExecuteScalar()
            End Using
        End Using

    End Sub

Open in new window

0
 

Author Closing Comment

by:PeterBaileyUk
ID: 41757420
thank you
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

650 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