Exemplo n.º 1
0
        public DBDataReader(string process, string sql, string connectionKey, Boolean singleRow, Boolean SingleValue)
        {
            var formattedSql = DBDataTable.FormatSql(sql);

            Log4Net.SqlStatementStack.Push(new Log4Net.SqlStatementStackMessage(formattedSql));
            Log4Net.LogDebug(formattedSql, connectionKey);

            Connection = StartUp.OpenConnection(connectionKey);

            DBCommand = new SqlCommand(formattedSql, Connection);
            DBCommand.CommandTimeout = StartUp.TimeOut;

            try
            {
                ReturnValue = DBCommand.ExecuteScalar();
            }
            catch
            {
                try
                {
                    if (Connection.State == ConnectionState.Closed)
                    {
                        Connection.Open();
                    }
                    ReturnValue = DBCommand.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    LogException(sql, "Scalar", ex, process);
                }
            }

            LogInfo(sql, "Scalar");
        }
Exemplo n.º 2
0
        public static void Transaction(ref string[] sql, string connectionKey)
        {
            var connection = StartUp.OpenConnection(connectionKey);

            SqlTransaction Trans;
            var            DBCommand = new SqlCommand();

            DBCommand.Connection = connection;

            Trans = connection.BeginTransaction();
            DBCommand.CommandTimeout = StartUp.TimeOut;
            DBCommand.Transaction    = Trans; int i = 0;

            try
            {
                for (i = 0; i < sql.Length; i++)
                {
                    if (sql[i] != null)
                    {
                        DBDataReader.MessageLog("SQL: " + sql[i]);
                        DBCommand.CommandText = sql[i];
                        DBCommand.ExecuteNonQuery();
                        Log4Net.LogInfo(sql[i], connectionKey);
                    }
                }

                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                Audit.HandleError("Sql Error", "SqlDML.Transaction(): Error running sql\r\n" + sql[i] + "\r\n" + ex.Message);
                Log4Net.LogError(sql[i], ex, connectionKey);
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }
Exemplo n.º 3
0
        public DBDataReader(string process, string sql, string connectionKey, Boolean singleRow)
        {
            var formattedSql = DBDataTable.FormatSql(sql);

            Log4Net.SqlStatementStack.Push(new Log4Net.SqlStatementStackMessage(formattedSql));
            Log4Net.LogDebug(formattedSql, connectionKey);

            Connection = StartUp.OpenConnection(connectionKey);

            DBCommand = new SqlCommand(formattedSql, Connection);
            DBCommand.CommandTimeout = StartUp.TimeOut;
            ReturnValue = string.Empty;

            try
            {
                DBReader = DBCommand.ExecuteReader(CommandBehavior.SingleRow);

                DBReader.Read();

                for (var i = 0; i < DBReader.FieldCount; i++)
                {
                    ReturnValue += DBReader[i] + "|";
                }
            }
            catch
            {
                try
                {
                    if (Connection.State == ConnectionState.Closed)
                    {
                        Connection.Open();
                    }
                    DBReader = DBCommand.ExecuteReader(CommandBehavior.SingleRow);
                }
                catch (Exception ex)
                {
                    LogException(formattedSql, String.Empty, ex, process);
                }
            }
        }
Exemplo n.º 4
0
        public DBDataReader(string process, string sql, string connectionKey)
        {
            var formattedSql = DBDataTable.FormatSql(sql);

            try
            {
                Log4Net.SqlStatementStack.Push(new Log4Net.SqlStatementStackMessage(formattedSql));
                Log4Net.LogDebug(formattedSql, connectionKey);

                Connection = StartUp.OpenConnection(connectionKey);

                DBCommand = new SqlCommand(formattedSql, Connection);
                DBCommand.CommandTimeout = StartUp.TimeOut;

                DBReader = DBCommand.ExecuteReader(CommandBehavior.Default);
            }
            catch (Exception ex)
            {
                LogException(formattedSql, String.Empty, ex, process);
            }

            LogInfo(sql, "Reader");
        }
Exemplo n.º 5
0
        public static void RunSQL(string process, string sql, string connectionKey)
        {
            sql = "\r\n" + sql.Replace("@", "\r\n @") + "\r\n";

            var connection = StartUp.OpenConnection(connectionKey);

            var dbCommand = new SqlCommand(sql);

            dbCommand.CommandTimeout = 24000;
            dbCommand.Connection     = connection;

            try
            {
                Debug.WriteLine(dbCommand.CommandText);

                dbCommand.ExecuteNonQuery();

                // sucessful log to file
                if (!sql.StartsWith("exec AuditDetailInsert") && !sql.StartsWith("exec AuditSummaryUpdate") && !sql.StartsWith("exec AuditSummaryInsert"))
                {
                    StartUp.FeedsAudit.AuditDetailInsert(process, "OK", "Ran statement: " + sql);
                }

                Log4Net.LogDebug(sql, connectionKey);
            }
            catch (Exception ex)
            {
                #region log exception to file
                var logEntry = "DML.RunSQL(): Error running SQL\r\n" + sql + "\r\n " + ex.Message + "\r\n";

                if (!sql.StartsWith("exec AuditDetailInsert") && !sql.StartsWith("exec AuditSummaryUpdate") && !sql.StartsWith("exec AuditSummaryInsert"))
                {
                    Audit.HandleError(process, logEntry);
                }
                else
                {
                    try
                    {
                        DBDataReader.MessageLog(logEntry + "\t" + DateTime.Now.ToLongDateString() + "\t" + DateTime.Now.ToLongTimeString());
                    }
                    catch { }
                }
                #endregion log excption to file

                Log4Net.LogError(sql, ex, connectionKey);

                var exp = new Exception(ex.Message + "\n" + sql, ex);

                throw exp;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }

            // sucessful log to file
            if (!sql.StartsWith("exec AuditDetailInsert") && !sql.StartsWith("exec AuditSummaryUpdate") && !sql.StartsWith("exec AuditSummaryInsert"))
            {
                DBDataReader.MessageLog("SQL: " + sql);
            }

            // var x = DateTime.Now;
            // var y = "Ran sql: " + sql + "\r\n";
            //StartUp.Create(sql);
        }
Exemplo n.º 6
0
        public static object RunScalarSQL(string process, string sql, string connectionKey)
        {
            object result;

            sql = "\r\n" + sql.Replace("@", "\r\n @") + "\r\n";

            var connection = StartUp.OpenConnection(connectionKey);

            var dbCommand = new SqlCommand(sql);

            dbCommand.CommandTimeout = 24000;
            dbCommand.Connection     = connection;
            //dbCommand.Parameters.Add("@NextValue", SqlDbType.Int).Direction = ParameterDirection.Output;

            try
            {
                result = dbCommand.ExecuteScalar();

                // sucessful log to file
                if (!sql.StartsWith("exec AuditDetailInsert") && !sql.StartsWith("exec AuditSummaryUpdate") && !sql.StartsWith("exec AuditSummaryInsert"))
                {
                    StartUp.FeedsAudit.AuditDetailInsert(process, "OK", "Ran statement: " + sql);
                }

                Log4Net.LogDebug(sql, connectionKey);
            }
            catch (Exception ex)
            {
                #region log exception to file
                var logEntry = "DML.RunSQL(): Error running SQL\r\n" + sql + "\r\n " + ex.Message + "\r\n";

                if (!sql.StartsWith("exec AuditDetailInsert") && !sql.StartsWith("exec AuditSummaryUpdate") && !sql.StartsWith("exec AuditSummaryInsert"))
                {
                    Audit.HandleError(process, logEntry);
                }
                else
                {
                    try
                    {
                        DBDataReader.MessageLog(logEntry + "\t" + DateTime.Now.ToLongDateString() + "\t" + DateTime.Now.ToLongTimeString());
                    }
                    catch { }
                }
                #endregion log excption to file

                Log4Net.LogError(sql, ex, connectionKey);

                var exp = new Exception(ex.Message + "\n" + sql, ex);

                throw exp;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }

            // sucessful log to file
            if (!sql.StartsWith("exec AuditDetailInsert") && !sql.StartsWith("exec AuditSummaryUpdate") && !sql.StartsWith("exec AuditSummaryInsert"))
            {
                DBDataReader.MessageLog("SQL: " + sql);
            }

            // var x = DateTime.Now;
            // var y = "Ran sql: " + sql + "\r\n";
            //StartUp.Create(sql);

            return(result);
        }