Click here to Skip to main content
15,892,809 members
Articles / Programming Languages / Visual Basic

Problem with GO statements in SQL scripts

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
13 Apr 2011CPOL 15.4K   4   5
How to workaround the problem with GO statements in SQL scripts.

Recently, I was working on an installer application which, as part of its job, would update the database based on a SQL script. When running such scripts from the .NET environment, you would get a SqlException as it does not like the GO statements within your script. The way around that is to split your script into pieces separated by GO and run each piece separately.

You also want to make sure that either all the pieces run successfully or none is run; otherwise, it would be hard to know how far the update has gone. Using a SqlTransaction ensures that if any problem happens, the transaction is rolled back and it only gets committed if there was no problem.

The code that I ended up using is shown below. It first does the segmentation of the original script using RegEx (defined in System.Text.RegularExpressions) and then defines a SqlTransaction for the whole update:

VB
Private Sub ExecuteSqlQuery()

    Dim i As Integer

    ' Read the sql file 
    Directory.SetCurrentDirectory(txtDownLoadLocation.Text)

    Dim fileStream As New FileStream(SqlFileName, FileMode.Open, FileAccess.Read)
    Dim streamReader As New StreamReader(fileStream)

    streamReader.BaseStream.Seek(0, SeekOrigin.Begin)

    Dim allSqlString As String = streamReader.ReadToEnd()

    streamReader.Close()
    fileStream.Close()

    ' Create segments of the string called "lines" each separated by "GO" 
    Dim regex As Regex = New Regex("^GO", RegexOptions.IgnoreCase)
    Dim lines As String() = Regex.Split(allSqlString)

    Dim connectionString As String = "Data Source=" & _
        txtDatabaseServer.Text & ";Integrated Security=SSPI;"
    Dim connection As SqlConnection = New SqlConnection(connectionString)
    Dim cmd As SqlCommand = New SqlCommand()
    cmd.Connection = connection

    connection.Open()

    ' Execute as a transaction, roll back if it fails
    Dim transaction As SqlTransaction = connection.BeginTransaction()
    cmd.Transaction = transaction

    For i = 0 To lines.Length - 1
        If lines(i).Length > 0 Then
            Try
                cmd.CommandText = lines(i)
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                transaction.Rollback()
                connection.Close()
                Exit Sub
            End Try
        End If
    Next

    transaction.Commit()
    connection.Close()
End Sub
This article was originally posted at http://morrisbahrami.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
I have over 17 years of experience in software development in a variety of fields. For last 7 years the emphasis has been mostly on .NET technology (C# and VB.NET) which includes WPF, WCF and ASP.NET AJAX. Also have SQL Server experience including SSIS and SSRS. My blog (http://morrisbahrami.blogspot.com) has a collection of tips and general info for Microsoft Developers.

Comments and Discussions

 
QuestionAdd RegexOptions.Multiline Pin
MurryGammash25-Dec-11 9:48
MurryGammash25-Dec-11 9:48 
GeneralMy observations Pin
Philippe Mori16-Apr-11 7:38
Philippe Mori16-Apr-11 7:38 
GeneralMy vote of 1 Pin
Ravi Sant14-Apr-11 0:53
Ravi Sant14-Apr-11 0:53 
General[My vote of 1] Good luck with that... Pin
Irasimus13-Apr-11 9:59
Irasimus13-Apr-11 9:59 
GeneralRe: [My vote of 1] Good luck with that... Pin
Morris Bahrami14-Apr-11 14:40
Morris Bahrami14-Apr-11 14:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.