SqlCommand() ExecuteNonQuery() truncates command text

后端 未结 6 1886
醉话见心
醉话见心 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条回答
  •  一向
    一向 (楼主)
    2020-12-11 15:44

    I ended up writing an implementation of a StringReader to do this.

    It handles:

    1. Skipping past GO contained in dash dash comments
    2. Skipping past GO contained in slash star comments
    3. Skipping past GO contained in literals (i.e single quotes)
    4. Skipping past GO contained in column names etc.

    Therefore it will only detect the keyword GO when used as a batch seperator. This means it splits the SQL text correctly.

    It also handles if you have appended a sql terminator (semicolon) to the word GO

    You can find the code for it here:

    You use it like so:

    using (var reader = new SqlCommandReader(scriptContents))
           {
                var commands = new List();
                reader.ReadAllCommands(c => commands.Add(c));
                // commands now contains each seperated sql batch.
            }
    

提交回复
热议问题