I have a SQL script with GO
batch separators that I am trying to execute from C# code. When I use Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery
, it works great. It blows up on Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteReader
or Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteWithResults
, though.
System.AggregateException: One or more errors occurred. --->
Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. --->
System.Data.SqlClient.SqlException: Incorrect syntax near 'GO'.
It's a very simple script that looks like this:
SELECT
DB_NAME()
AS
'Database Name';
GO
EXEC [schema].[MyStoredProcedure];
GO
SELECT
[Column]
AS
'Friendly Column Name'
FROM
[schema].[Table];
GO
I have tried script.Replace("\n", "\r\n")
and I get the same results. Does anyone know how I can execute a script like this and get the results back?