SqlCommand() ExecuteNonQuery() truncates command text

后端 未结 6 1898
醉话见心
醉话见心 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:27

    This is what we use :)

    public static class ExtensionMethodsSqlCommand
    {
        #region Public
    
        private static bool IsGo(string psCommandLine)
        {
            if (psCommandLine == null)
                return false;
            psCommandLine = psCommandLine.Trim();
            if (string.Compare(psCommandLine, "GO", StringComparison.OrdinalIgnoreCase) == 0)
                return true;
            if (psCommandLine.StartsWith("GO", StringComparison.OrdinalIgnoreCase))
            {
                psCommandLine = (psCommandLine + "--").Substring(2).Trim();
                if (psCommandLine.StartsWith("--"))
                    return true;
            }
            return false;
        }
    
        [System.Diagnostics.DebuggerHidden]
        public static void ExecuteNonQueryWithGos(this SqlCommand poSqlCommand)
        {
            string sCommandLong = poSqlCommand.CommandText;
            using (StringReader oStringReader = new StringReader(sCommandLong))
            {
                string sCommandLine;
                string sCommandShort = string.Empty;
                while ((sCommandLine = oStringReader.ReadLine()) != null)
                    if (ExtensionMethodsSqlCommand.IsGo(sCommandLine))
                    {
                        if (sCommandShort.IsNullOrWhiteSpace() == false)
                        {
                            if ((poSqlCommand.Connection.State & ConnectionState.Open) == 0)
                                poSqlCommand.Connection.Open();
                            using (SqlCommand oSqlCommand = new SqlCommand(sCommandShort, poSqlCommand.Connection))
                                oSqlCommand.ExecuteNonQuery();
                        }
                        sCommandShort = string.Empty;
                    }
                    else
                        sCommandShort += sCommandLine + "\r\n";
            }
        }
    
        #endregion Public
    }
    

提交回复
热议问题