|
I have used the example below inside my MSSQL2005 class, I just wondered if there was an easier way. At the end of the day, I didn't want to have to modify the sql scripts coming from sql server 2005 manager (just being lazy really). The transaction is handled to ensure that the script executes in full or not at all. Thanks for your input chaps.
Regards
Mark
Public Overrides Sub Execute_SqlScript(ByVal fileName As String, ByVal database As String)
Dim sr As StreamReader = Nothing
Dim sb As StringBuilder = Nothing
Dim line As String = ""
Try
Command_Type = CommandType.Text
Command_Create()
Connection_Open()
Transaction_Begin()
sr = New StreamReader(fileName)
Do
sb = New StringBuilder
Do
line = sr.ReadLine()
If (line = "GO" Or line Is Nothing) Then Exit Do
sb.Append(ControlChars.CrLf & line)
Loop
If line Is Nothing Then Exit Do
objSQLCommand.CommandText = sb.ToString
objSQLCommand.ExecuteNonQuery()
Loop Until line Is Nothing
If Me.Transaction_Exists Then Me.Transaction_Commit()
Catch ex As Exception
Transaction_Rollback()
Throw New aExceptions.BespokeException(ex, _
aExceptions.BespokeException.enmType.SERVER_SQLERROR, _
"MSSQL2005: Execute_SqlScript(2)")
Finally
If sr IsNot Nothing Then sr.Close()
Reset()
End Try
End Sub |