Hi,
I am attempting to execute SQL Server script files from C#. Some of the scripts create stored procedures, tables, views etc - this C# program is intended to be able to create a "clean" database from scripts kept in our version control system.
Most of our scripts attempt to determine if the object in the script file exists by doing:
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[foo]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
DROP PROCEDURE [dbo].[foo]
END
GO
CREATE PROCEDURE foo
.
.
.
I read the contents of the script file into the SqlCommand.CommandText and then use ExecuteNonQuery.
But, when I do that I am throwing an execption:
Line 1: Incorrect syntax near 'GO'.
'CREATE PROCEDURE' must be the first statement in a query batch.
If I run the same script directly in Query Analyzer, the script runs without error.
If anyone could explain to me what I am doing wrong, I would be most appreciative.
Thanks in advance for any help.
/dave
I am attempting to execute SQL Server script files from C#. Some of the scripts create stored procedures, tables, views etc - this C# program is intended to be able to create a "clean" database from scripts kept in our version control system.
Most of our scripts attempt to determine if the object in the script file exists by doing:
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[foo]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
DROP PROCEDURE [dbo].[foo]
END
GO
CREATE PROCEDURE foo
.
.
.
I read the contents of the script file into the SqlCommand.CommandText and then use ExecuteNonQuery.
But, when I do that I am throwing an execption:
Line 1: Incorrect syntax near 'GO'.
'CREATE PROCEDURE' must be the first statement in a query batch.
If I run the same script directly in Query Analyzer, the script runs without error.
If anyone could explain to me what I am doing wrong, I would be most appreciative.
Thanks in advance for any help.
/dave