Home All Groups Group Topic Archive Search About

Running .sql file using C#

Author
14 Jul 2006 9:16 AM
Gislain
Hi,

I'm trying to run .sql file with C# code, but i have systematically an error
message with the "GO" instruction. When i test the script in SQL Server
Management Studio, it work fine !!!


First part of the error message
----------------------------------------
{System.Data.SqlClient.SqlException: Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection)
etc...

SQL file to be played (SET NOEXEC ON is needed !!!)
--------------------------------------------------------------------------------
    SET NOEXEC ON
    GO
    DROP PROCEDURE spr_gr_test
    GO
    CREATE PROCEDURE spr_gr_test
    as
    begin
         PRINT 'titi'
    end
    GO


C# code
----------------------------------------
            SqlCommand __cmd = myConnection.CreateCommand();
            StreamReader __streamReader = null;
            string __myQuery = "";
            int __rc = 0;

            try
            {
                __streamReader = new
StreamReader(@"C:\Data\Dvlp\SQL.2005\CheckScript\SQLQuery1.sql");

                __myQuery = __streamReader.ReadToEnd();
                __cmd.CommandType = CommandType.Text;
                __cmd.CommandText = __myQuery;
                __rc = __cmd.ExecuteNonQuery();
                Debug.WriteLine(__rc.ToString());
            }
            catch (Exception exp)
            {
                Debug.WriteLine(exp.ToString());
            }
            finally
            {
                if (__streamReader != null)
                    __streamReader.Close();
            }




Where is the solution ?
Where is the mistake .....

Thank's by advance

Gislain

Author
14 Jul 2006 9:29 AM
ML
"GO" is a keyword known only to SQL Server Tools (AFAIK). It's not a T-SQL
keyword, but just a delimiter used by QA/SSMS to allow you to separate
individual queries and/or object definitions from others.

Replace with semicolon (";") in other programming langages unless semicolon
is already present.


ML

---
http://milambda.blogspot.com/
Author
17 Jul 2006 3:33 PM
Keith Kratochvil
A semicolon is not the same as GO.  A semicolon is not treated as a batch
separator.  For example, you can create a stored procedure witih a semicolon
in it.  Try putting a GO within a stored procedure and it is treated as a
batch separator and it ends the CREATE PROCEDURE batch.

--
Keith Kratochvil


Show quote
"ML" <M*@discussions.microsoft.com> wrote in message
news:759CE351-7717-49FA-98E5-B01D341ABC02@microsoft.com...
> "GO" is a keyword known only to SQL Server Tools (AFAIK). It's not a T-SQL
> keyword, but just a delimiter used by QA/SSMS to allow you to separate
> individual queries and/or object definitions from others.
>
> Replace with semicolon (";") in other programming langages unless
> semicolon
> is already present.
>
>
> ML
>
> ---
> http://milambda.blogspot.com/
Author
17 Jul 2006 3:51 PM
ML
That's true. If it seems as if I implied otherwise, know that it was not
intentional.


ML

---
http://milambda.blogspot.com/

AddThis Social Bookmark Button