0

I want to update a table through sql code executed in a c# application. To do this, I've used the alter data generated my MSSMS and manually saved it as an sql-file. The c# then reads the file and tries to execute it but it can't. If I use the sql code by itself it works, but not when read by the c# funtion. What's wrong with my c# code?

The sql code generated my MSSMS:

/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.tTest ADD
    NewColumn int NULL
GO
ALTER TABLE dbo.tTest SET (LOCK_ESCALATION = TABLE)
GO
COMMIT

The c# code that reads it:

string content = string.Empty;
try
{
    content = File.ReadAllText(string.Format(@"C:\temp\{0}.sql", name));
    SqlConnection conn = new SqlConnection(ConnectionString);
    SqlCommand command = new SqlCommand(content, conn);
    command.Connection.Open();
    command.ExecuteNonQuery();
    command.Connection.Close();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

The output that comes from the c# function (the console message):

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.

2 Answers 2

2

Each batch (ended with GO) should be sent separately in one command.ExecuteNonQuery(). This method is not to be used for multiple batches.

Split your query into several pieces (where GO is) and execute it step by step.

Sign up to request clarification or add additional context in comments.

Comments

0

The core issue with the script you are trying to run is wrapping a transaction around query batches (A query batch is terminated with a GO statement in SQL Server Management Studio).

To do the operations in C# you can execute both statements in one query batch. They do not require to be separate. If you wrap them in one query in a SqlCommand object you do not need to handle transactions as there is an "implicit" transaction created.

A last point to look out for is proper disposal of the objects implementing IDisposable. The easiest way to do this in C# is by wrapping them in a using clause. Once you did that, there is no more need to call the Close method on the command/connection objects.

Combining all these remarks gives you the following code:

        try
        {
            using(var conn = new SqlConnection(ConnectionString))
            using(var command = new SqlCommand(
                    @"ALTER TABLE dbo.tTest ADD NewColumn int NULL;
                    ALTER TABLE dbo.tTest SET (LOCK_ESCALATION = TABLE);", conn))
            {
                conn.Open();
                command.ExecuteNonQuery();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

1 Comment

The sql code I had is autogenerated from MSSQL and I dont wish people to edit it for every update of the db

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.