Results 1 to 8 of 8

Thread: Running 3 stored procedures from VB.Net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Running 3 stored procedures from VB.Net

    I have 3 stored procedures that need to be run from my code.

    sp_Proc_01
    sp_Proc_02
    sp_Proc_03

    The first deletes all the records in a database and the next two (actually there are 20 in the application) rebuild the table with new information.

    How do I write code to run this series of SPs? An example or link would be greatly appreciated. Thanks.

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    search on the sqlCommand object

    at least have an attempt at coding it, then we'll give you a hand!
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Of course I did a search before I posted (I searched here and on Google). I cannot find anything related to running stored procedures from VB.Net code. I did find a lot of info about running stored procedures that add records to tables in ASP.Net using parameters but that doesn't apply.

    I would love show some code but I don't even know where to start, which is why I asked for in sample or a link to a tutorial.

    Thanks anyways

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    ok,

    Code:
    dim myCommand as new sqlCommand
    
    myCommand.commandType = storedproc
    myCommand.commandText = "nameofstoredproc"
    myCommand.connection = mySQLConnection
    myCommand.ExecuteNonQuery
    done from memory and not tested!
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    I am still stumped on this.
    VB Code:
    1. Private Sub btnUpdateRates_Click(ByVal sender As System.Object, _
    2.         ByVal e As System.EventArgs) Handles btnUpdateRates.Click
    3.  
    4.         Dim cmd As New SqlCommand
    5.         cmd.CommandType = CommandType.StoredProcedure
    6.         cmd.CommandText = "sp_AC1_ST"
    7.         cmd.Connection = strCon
    8.         cmd.ExecuteNonQuery()
    9.  
    10.     End Sub
    The strCon is underlined with this warning. Value of type 'String' cannot be converted to System.Data.SqlClient.SqlConnection.

    I know it seems like I am asking you to finish my project for me but I don't know how else to finish this (should be) simple prodedure.

  6. #6
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    strcon should be a connection object not connection string
    Code:
    dim myConnection as new sqlConnection(strCon)
    
    myCommand.Connection = myConnection
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    That worked. Thank you very much. One more question. Is there a better/shorter way to do the following?
    VB Code:
    1. Dim cmd As New SqlCommand
    2.         Dim con As New SqlConnection(strCon)
    3.         cmd.CommandType = CommandType.StoredProcedure
    4.  
    5.         ' Clear old data |***************************
    6.         cmd.CommandText = "sp_DeleteRates"
    7.         cmd.Connection = con
    8.         con.Open()
    9.         cmd.ExecuteNonQuery()
    10.         con.Close()
    11.         cmd.Dispose()
    12.  
    13.         ' AC1 using strait time |********************
    14.         cmd.CommandText = "sp_AC1_ST"
    15.         cmd.Connection = con
    16.         con.Open()
    17.         cmd.ExecuteNonQuery()
    18.         con.Close()
    19.         cmd.Dispose()
    20.  
    21.         ' AC1 using over time |***********************
    22.         cmd.CommandText = "sp_AC1_OT"
    23.         cmd.Connection = con
    24.         con.Open()
    25.         cmd.ExecuteNonQuery()
    26.         con.Close()
    27.         cmd.Dispose()
    28.  
    29.         ' AC2 using over time |***********************
    30.         cmd.CommandText = "sp_AC2_OT"
    31.         cmd.Connection = con
    32.         con.Open()
    33.         cmd.ExecuteNonQuery()
    34.         con.Close()
    35.         cmd.Dispose()
    I think I a need a With Cmd / End With but I am not sure where to put it to run all my seperate sps.

    Thanks again.

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here,
    VB Code:
    1. Private Sub StartProcess()
    2.    ' Clear old data |***************************  
    3.    RunCommand("sp_DeleteRates", strCon)
    4.    ' AC1 using strait time |********************
    5.    RunCommand("sp_AC1_ST", strCon)
    6.    ' AC1 using over time |***********************
    7.    RunCommand("sp_AC1_OT", strCon)
    8.    ' AC2 using over time |***********************
    9.    RunCommand("sp_AC2_OT", strCon)
    10. End Sub
    11.  
    12.  
    13. Private Sub RunCommand(strProcedure As String, strCon As String)
    14.         Dim cmd As New SqlCommand
    15.         Dim con As New SqlConnection(strCon)
    16.         cmd.CommandType = CommandType.StoredProcedure
    17.  
    18.         cmd.CommandText = strProcedure
    19.         cmd.Connection = con
    20.         con.Open()
    21.         cmd.ExecuteNonQuery()
    22.         con.Close()
    23.         cmd.Dispose()
    24. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width