SqlCommand() ExecuteNonQuery() truncates command text.

Posted by H. Abraham Chavez on Stack Overflow See other posts from Stack Overflow or by H. Abraham Chavez
Published on 2010-03-16T17:15:16Z Indexed on 2010/03/16 20:01 UTC
Read the original article Hit count: 391

Filed under:
|

I'm building a custom db deployment utility, I need to read text files containing sql scripts and execute them against the database.

Pretty easy stuff, so far so good.

However I've encountered a snag, the contents of the file are read successfully and entirely, but once passed into the SqlCommand and then executed with SqlCommand.ExecuteNonQuery only part of the script is executed.

I fired up Profiler and confirmed that my code is not passing all of the script.

    private void ExecuteScript(string cmd, SqlConnection sqlConn, SqlTransaction trans)
    {

        SqlCommand sqlCmd = new SqlCommand(cmd, sqlConn, trans);
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandTimeout = 9000000; // for testing
        sqlCmd.ExecuteNonQuery();

    }

    // I call it like this, readDMLScript contains 543 lines of T-SQL
    string readDMLScript = ReadFile(dmlFile);
    ExecuteScript(readDMLScript, sqlConn, trans);

© Stack Overflow or respective owner

Related posts about sqlclient

Related posts about sqlcommand