I'm currently using SMO to help automate the patching and/or upgrading of customer databases that are in production. I'm using the following code
Public Sub RunScript(ByVal db As Database, ByVal scriptInfo As FileInfo)
If scriptInfo.Exists Then
RaiseEvent LogMessage("Executing Indicated Script: " & scriptInfo.Name)
Using sr As New StreamReader(scriptInfo.FullName)
Dim script As String = sr.ReadToEnd
Try
db.ExecuteNonQuery(script)
Catch ex As Exception
RaiseEvent LogMessage("ERROR: " & ex.Message.ToString)
db.ExecuteNonQuery(script,
Microsoft.SqlServer.Management.Common.ExecutionTypes.ContinueOnError)
End Try
End Using
End If
End Sub
What I'm doing is logging the SQLConnection messages that are coming off of the SQLConneciton that I am using with the Database part that give me fairly simplistic messages about what is happening. Am I'm also Logging myself various messages through the LogMessage(string) event that I have created.
What I would really like to be able to do is execute the scripts so that the continue if there is an error, but still log that there is an error without having to do it the way I am now.
The reason is some of the errors that break the transactions are trivial and it's okay that they are happening. Like trying to create an index when one exists or what-have-you.
Is there any way to both continue script execution on error but still log the error message?
*Edit * A Clarification for what the two objects coming in are
The scriptInfo is a FileInfo object that tells the StreamReader where to load the sql script from.
The db is a SMO Database object that is associated with a SQLClient.Connection that allows me to execute the script on the database that is associated with db
The error logging is happening on the SqlClient.SqlConnections OnInfo Event, or from the LogMessage event