SqlCommand() ExecuteNonQuery() truncates command text

后端 未结 6 1885
醉话见心
醉话见心 2020-12-11 14:48

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 g

6条回答
  •  萌比男神i
    2020-12-11 15:38

    I found the code below while searching for an answer to this issue:

    http://blogs.msdn.com/b/onoj/archive/2008/02/26/incorrect-syntax-near-go-sqlcommand-executenonquery.aspx

    Pros: It's short and simple to understand and worked perfectly for my needs.

    Cons: It is less efficient than Stream based solutions and is case sensitive (i.e "GO" not "go").

    string[] commands = sql.Split(new string[]{"GO\r\n", "GO ", "GO\t"}, StringSplitOptions.RemoveEmptyEntries );
    foreach (string c in commands)
    {
        var command = new SqlCommand(c, masterConnection);
        command.ExecuteNonQuery();
    }
    

提交回复
热议问题