public static void ExecuteNonQueries(this DbConnection conn, string sql, DbTransaction tran, int?timeout)
 {
     foreach (string cmd in GoSplitter.GoSplit(sql))
     {
         ExecuteNonQuery(conn, cmd, tran, timeout);
     }
 }
 public static void ExecuteNonQueries(this DbConnection conn, string sql)
 {
     foreach (string cmd in GoSplitter.GoSplit(sql))
     {
         ExecuteNonQuery(conn, cmd);
     }
 }
Example #3
0
        private void RunScript(TextReader reader, DbConnection conn, DbTransaction tran, bool replace, bool logEachQuery, bool logCount, IShellContext context)
        {
            int count = 0;

            foreach (string item in GoSplitter.GoSplit(reader))
            {
                string sql = item;
                if (replace)
                {
                    sql = context.Replace(sql, ReplacePattern);
                }
                var cmd = conn.CreateCommand();
                cmd.CommandText    = sql;
                cmd.CommandTimeout = Timeout;
                cmd.Transaction    = tran;
                try
                {
                    if (logEachQuery)
                    {
                        context.GetLogger <Script>().LogInformation("DBSH-00064 Executing SQL command {sql}", sql);
                    }
                    cmd.ExecuteNonQuery();
                }
                catch (Exception err)
                {
                    context.GetLogger <Script>().LogError(0, err, "DBSH-00065 Error when executing script {1}", sql);
                    throw;
                }
                count++;
            }
            if (logCount)
            {
                context.GetLogger <Script>().LogInformation("DBSH-00073 Executed {0} commands", count);
            }
        }
        public static void RunSqlCommands(this DbConnection conn, string sql)
        {
            DbTransaction trans = null;

            try
            {
                trans = conn.BeginTransaction();
            }
            catch (Exception)
            {
                trans = null;
            }

            try
            {
                foreach (string sql_item in GoSplitter.GoSplit(sql))
                {
                    using (DbCommand c = conn.CreateCommand())
                    {
                        if (trans != null)
                        {
                            c.Transaction = trans;
                        }
                        c.Connection  = conn;
                        c.CommandText = sql_item;
                        c.ExecuteNonQueryEx();
                    }
                }
            }
            catch
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
                throw;
            }

            if (trans != null)
            {
                trans.Commit();
            }
        }