ScriptUtility
The ScriptUtility class runs a SQL script against a SQL Server database. It splits the script into batches at each GO line and runs them in order, keeping track of line numbers so errors point to the right place in the script.
To use it, reference the CodeSmith.BaseTemplates assembly and import its namespace:
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
ScriptUtility has one static method, ExecuteScript, with two overloads:
| Overload | Description |
|---|---|
ExecuteScript(string connectionString, string script) |
Runs the script against the database and returns a ScriptResult. |
ExecuteScript(string connectionString, string script, SqlInfoMessageEventHandler infoMessageHandler) |
Does the same, and passes informational messages from SQL Server to your handler. |
The returned ScriptResult has a Success property and an Errors collection, and its ToString() method summarizes the outcome, so you can write it to the trace output.
For a complete example, see Auto-executing generated SQL scripts.