Executing batches of TSQL statements containing GO

The GO statement of the Sql Server Management Studio is not understood by the SqlClient and it will refuse ro run the script.

The Server class of SQL Server Management Objects can understand the GO statement and thus can execute the script.

To use this you need to include three references:

  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.ConnectionInfo
  • Microsoft.SqlServer.Management.Sdk.Sfc

The code used is :

string connectionString,sqlScript;
SqlConnection sqlConnection;
ServerConnection serverConnection;
Server server;
sqlConnection = new SqlConnection(connectionString);
serverConnection = new ServerConnection(sqlConnection);
server = new Server(serverConnection);
catch (Microsoft.SqlServer.Management.Common.ExecutionFailureException e)
// Do something intelligent when the error occurs

The information here is grabbed from this article: Original post by Syed Mehroz Alam

Leave a Reply

Your email address will not be published. Required fields are marked *