Example #1
0
        private string ExecuteScriptInternal(string connectionString, string script)
        {
            string exceptions = "";

            string[] sqlStatements = SqlDelimiterRegex.Split(script);
            foreach (string statement in sqlStatements)
            {
                var sql = statement.Trim();
                if (!String.IsNullOrEmpty(sql))
                {
                    // script dynamic substitution
                    sql = DataUtil.ReplaceTokens(sql);

                    //Clean up some SQL Azure incompatabilities
                    var query = GetAzureCompactScript(sql);

                    if (query != sql)
                    {
                        var props = new LogProperties {
                            new LogDetailInfo("SQL Script Modified", query)
                        };

                        var elc = new EventLogController();
                        elc.AddLog(props,
                                   PortalController.GetCurrentPortalSettings(),
                                   UserController.GetCurrentUserInfo().UserID,
                                   EventLogController.EventLogType.HOST_ALERT.ToString(),
                                   true);
                    }

                    try
                    {
                        Logger.Trace("Executing SQL Script " + query);

                        //Create a new connection
                        var connection = new SqlConnection(connectionString);
                        //Create a new command (with no timeout)
                        var command = new SqlCommand(query, connection)
                        {
                            CommandTimeout = 0
                        };

                        connection.Open();
                        command.ExecuteNonQuery();
                        connection.Close();
                    }
                    catch (SqlException objException)
                    {
                        Logger.Debug(objException);

                        exceptions += objException + Environment.NewLine + Environment.NewLine + query + Environment.NewLine + Environment.NewLine;
                    }
                }
            }

            return(exceptions);
        }
Example #2
0
        private static void RunScript(string script)
        {
            script = script.Replace("{objectQualifier}", DatabaseEnvironment.ObjectQualifier).Replace("{databaseOwner}", DatabaseEnvironment.DatabaseOwner);

            var scripts = SqlDelimiterRegex.Split(script);

            foreach (var item in scripts)
            {
                using (var conn = new SqlConnection(DatabaseEnvironment.ConnectionString))
                {
                    var cmd = new SqlCommand(item, conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }