Subtext.Scripting.SqlScriptRunner.Execute C# (CSharp) Method

Execute() public method

Executes the script.
Use script.Execute(transaction) to do the work. We will also pull the status of our script exection from here.
public Execute ( SqlTransaction transaction ) : int
transaction System.Data.SqlClient.SqlTransaction The current transaction.
return int
        public int Execute(SqlTransaction transaction)
        {
            int recordsAffectedTotal = 0;
            SetNoCountOff(transaction);

            // the following reg exp will be used to determine if each script is an
            // INSERT, UPDATE, or DELETE operation. The reg exp is also only looking
            // for these actions on the SubtextData database. <- do we need this last part?
            string regextStr = @"(INSERT\sINTO\s[\s\w\d\)\(\,\.\]\[\>\<]+)|(UPDATE\s[\s\w\d\)\(\,\.\]\[\>\<]+SET\s)|(DELETE\s[\s\w\d\)\(\,\.\]\[\>\<]+FROM\s[\s\w\d\)\(\,\.\]\[\>\<]+WHERE\s)";
            Regex regex = new Regex(regextStr, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);

            scripts.ApplyTemplatesToScripts();
            foreach(Script script in scripts)
            {
                int returnValue = script.Execute(transaction);

                Match match = regex.Match(script.ScriptText);
                if (match.Success)
                {
                    /*
                     * For UPDATE, INSERT, and DELETE statements, the return value is the
                     * number of rows affected by the command. For all other types of statements,
                     * the return value is -1. If a rollback occurs, the return value is also -1.
                     */
                    if (!IsCrudScript(script))
                        continue;

                    if (returnValue > -1)
                    {
                        recordsAffectedTotal += returnValue;
                    }
                    else
                    {
                        throw new SqlScriptExecutionException("An error occurred while executing the script.", script, returnValue);
                    }
                }
            }
            return recordsAffectedTotal;
        }

Usage Example

Example #1
0
 /// <summary>
 /// Executes the script.
 /// </summary>
 /// <remarks>
 /// Use script.Execute(transaction) to do the work. We will also pull the
 /// status of our script exection from here.
 /// </remarks>
 /// <param name="scriptName">Name of the script.</param>
 /// <param name="transaction">The current transaction.</param>
 /// <param name="dbUserName">Name of the DB owner.</param>
 public static void ExecuteScript(string scriptName, SqlTransaction transaction, string dbUserName)
 {
     SqlScriptRunner scriptRunner = new SqlScriptRunner(UnpackEmbeddedScript(scriptName), Encoding.UTF8);
     if(!string.IsNullOrEmpty(dbUserName))
         scriptRunner.TemplateParameters.SetValue("dbUser", dbUserName);
     scriptRunner.Execute(transaction);
 }
All Usage Examples Of Subtext.Scripting.SqlScriptRunner::Execute