Chronozoom.Entities.ManualMigrationCheck.ExecCENativeSQL C# (CSharp) Метод

ExecCENativeSQL() приватный Метод

Use to run T-SQL scripts for SQL Server CE which can't be run through DbCommand since they include native, non-standard commands such as "GO". As there is no CE equivilent to ServerConnection, we will chunk the script into segments seperated by each GO and make each segment a unique DbCommand call. We will also remove the key/index ASC option, CLUSTERED option, [dbo] schema name, WITH CHECK parameter from any script since these are not supported, and convert the (MAX) option to an appropriate value and GetUTCDate to GetDate since these is not supported either. Additionally, any chunk containing a trigger will be completely ignored (the entire chunk) and not run since triggers are not supported. Thus this function is also a translator for CE scripts.
private ExecCENativeSQL ( string sqlScript, SqlCeConnection sqlConnection ) : void
sqlScript string The text of the script to be run.
sqlConnection SqlCeConnection The SQLCeConnection to run the script against, which should already be established.
Результат void
        private void ExecCENativeSQL(string sqlScript, SqlCeConnection sqlConnection)
        {
            string[] sqlScriptChunks = sqlScript
                .Replace(" ASC,", ",")
                .Replace(" ASC\r\n", "\r\n")
                .Replace(" CLUSTERED", "")
                .Replace("[dbo].", "")
                .Replace("GetUTCDate()", "GetDate()")
                .Replace("GETUTCDATE()", "GETDATE()")
                .Replace("[nvarchar](max)",  "[nvarchar](4000)")
                .Replace("nvarchar(max)",    "nvarchar(4000)")
                .Replace("[varbinary](max)", "[varbinary](8000)")
                .Replace("varbinary(max)",   "varbinary(8000)")
                .Replace("CONVERT(VARBINARY(MAX)", "CONVERT(VARBINARY")
                .Replace("WITH CHECK", "")
                .Split(new string[] { "\nGO", "\nGo", "\ngo" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string sqlScriptChunk in sqlScriptChunks)
            {
                // only execute if script doesn't contain trigger code and is more than blank space/lines
                if
                (
                    (!sqlScriptChunk.Contains(" TRIGGER ")) &&
                    (sqlScriptChunk.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "") != "")
                )
                {
                    try
                    {
                        using (SqlCeCommand cmd = new SqlCeCommand(sqlScriptChunk, sqlConnection))
                        {
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        // ensure full text of SQL chunk is included in any error details
                        // - useful for when testing translation to CE-compatible script
                        throw new Exception("ExecCENativeSQL Chunk:\n\n" + sqlScriptChunk, ex);
                    }
                }
            }
        }