none
How to execute multiple sql statements/queries on one button click in vb.net RRS feed

  • Question

  • Greetings!!

    Hi, Im beginner in vb.net and I just wanna ask if how can I execute multiple sql queries inside IF END IF.

    EXAMPLE

    IF MsgBox("Release Item?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "") = MsgBoxResult.Yes Then

    IF val(paid) = val(totalcost) then

    query = "INSERT INTO tbl_example()VALUES(...,...)"

    query2 = "INSERT INTO tbl_add()VALUES(...,...,...)"

    END IF

    END IF

    '* paid, totalcost, query, query2 are variables

    Friday, September 19, 2014 1:42 PM

Answers

  • I generally recommend using Parameters but it is also possible to put all of your action queries in a single statement:

    Public Sub CreateCommand(ByVal connectionString As String)
        Using connection As New SqlConnection(connectionString)
            Dim query As String = "INSERT INTO tbl_example ..."
            Dim query2 As String = "INSERT INTO tbl_add ..." 
            Dim QueryString As String = String.Concat(query,";",query2)
            Dim command As New SqlCommand(QueryString, connection)
            command.Connection.Open()
            command.ExecuteNonQuery()
        End Using 
    End Sub
    


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, September 19, 2014 6:53 PM
  • http://www.dotnetheaven.com/article/ado.net-insert-command-in-vb.net

    Dim com1 AsNew SqlCommand(ins1, con)

     com1.ExecuteNonQuery()

    Dim com2 AsNew SqlCommand(ins2, con)

    com2.ExecuteNonQuery()

    You will figure out how to execute the inserts back to back by executing  com.ExecuteNonQuery() twice.

    Friday, September 19, 2014 2:33 PM

All replies

  • http://www.dotnetheaven.com/article/ado.net-insert-command-in-vb.net

    Dim com1 AsNew SqlCommand(ins1, con)

     com1.ExecuteNonQuery()

    Dim com2 AsNew SqlCommand(ins2, con)

    com2.ExecuteNonQuery()

    You will figure out how to execute the inserts back to back by executing  com.ExecuteNonQuery() twice.

    Friday, September 19, 2014 2:33 PM
  • Imports System.Data.SqlClient
    Module Module1
        Sub Main()
            Dim x As Integer = 1
            If x = 1 Then
                Try
                    Using con As New SqlConnection("TheConnectionstringToTheDataBase")
                        con.Open()
                        Using com As New SqlCommand("Whatever SQL String", con)
                            com.ExecuteNonQuery()
                            com.CommandText = "Whatever SQL String"
                            com.ExecuteNonQuery()
                        End Using
                    End Using
                Catch ex As Exception
                    'Do error handling 
                End Try
            End If
        End Sub
    End Module


    Success
    Cor

    Friday, September 19, 2014 3:05 PM
  • I generally recommend using Parameters but it is also possible to put all of your action queries in a single statement:

    Public Sub CreateCommand(ByVal connectionString As String)
        Using connection As New SqlConnection(connectionString)
            Dim query As String = "INSERT INTO tbl_example ..."
            Dim query2 As String = "INSERT INTO tbl_add ..." 
            Dim QueryString As String = String.Concat(query,";",query2)
            Dim command As New SqlCommand(QueryString, connection)
            command.Connection.Open()
            command.ExecuteNonQuery()
        End Using 
    End Sub
    


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, September 19, 2014 6:53 PM
  • Thank you Paul... You saved my day.. I have been wondering how to do this for hours together.

    Thanks again.

    Thursday, October 31, 2019 5:54 AM