Ejemplo n.º 1
0
 public static int ExecuteNonQuery(DbTransaction trans, CommandType cmdType, string cmdText,
                                   params DbParameter[] commandParameters)
 {
     try
     {
         DbCommand cmd = Provider.CreateCommand();
         PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
         int val = cmd.ExecuteNonQuery();
         if (cmdType == CommandType.StoredProcedure)
         {
             val = (int)cmd.Parameters[cmd.Parameters.Count - 1].Value;
         }
         cmd.Parameters.Clear();
         return(val);
     }
     catch (Exception ex)
     {
         IDbConnection connection = trans.Connection;
         if (connection != null && !string.IsNullOrEmpty(connection.ConnectionString))
         {
             string conStr   = connection == null ? "dbConnection为空" : string.IsNullOrEmpty(connection.ConnectionString) == true ? "ConnectionString为空" : connection.ConnectionString;
             string errorMsg = string.Format("{0};{1};{2};{3};{4};{5}", cmdText, commandParameters.GetParamKeyValues(), connection == null ? conStr : conStr.Substring(0, Regex.Matches(conStr, ";")[1].Index), ex.Message, ex.Source, ex.StackTrace);
             Logger.Fatal(errorMsg, ex);
             DBLog.Process("", "", ClientHelper.GetClientIP(), "", "", "sql_exception", errorMsg);
         }
         throw ex;
     }
 }
Ejemplo n.º 2
0
 public static DataTable ExecuteTable(DbConnection connection, CommandType cmdType, string cmdText, params DbParameter[] commandParameters)
 {
     try
     {
         DbCommand cmd = Provider.CreateCommand();
         PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
         DbDataAdapter ap = Provider.CreateDataAdapter();
         ap.SelectCommand = cmd;
         DataSet st = new DataSet();
         ap.Fill(st, "Result");
         cmd.Parameters.Clear();
         return(st.Tables["Result"]);
     }
     catch (Exception ex)
     {
         if (connection != null && !string.IsNullOrEmpty(connection.ConnectionString))
         {
             string conStr   = connection == null ? "dbConnection为空" : string.IsNullOrEmpty(connection.ConnectionString) == true ? "ConnectionString为空" : connection.ConnectionString;
             string errorMsg = string.Format("{0};{1};{2};{3};{4};{5}", cmdText, commandParameters.GetParamKeyValues(), connection == null ? conStr : conStr.Substring(0, Regex.Matches(conStr, ";")[1].Index), ex.Message, ex.Source, ex.StackTrace);
             Logger.Fatal(errorMsg, ex);
             DBLog.Process("", "", ClientHelper.GetClientIP(), "", "", "sql_exception", errorMsg);
         }
         throw ex;
     }
 }
Ejemplo n.º 3
0
        public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
        {
            try
            {
                SqlCommand command = new SqlCommand();
                PrepareCommand(command, connection, null, commandType, commandText, commandParameters);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet        dataSet = new DataSet();
                adapter.Fill(dataSet);
                command.Parameters.Clear();

                return(dataSet);
            }
            catch (Exception ex)
            {
                if (connection != null && !string.IsNullOrEmpty(connection.ConnectionString))
                {
                    string conStr   = connection == null ? "dbConnection为空" : string.IsNullOrEmpty(connection.ConnectionString) == true ? "ConnectionString为空" : connection.ConnectionString;
                    string errorMsg = string.Format("{0};{1};{2};{3};{4};{5}", commandText, commandParameters.GetParamKeyValues(), connection == null ? conStr : conStr.Substring(0, Regex.Matches(conStr, ";")[1].Index), ex.Message, ex.Source, ex.StackTrace);
                    Logger.Fatal(errorMsg, ex);
                    DBLog.Process("", "", ClientHelper.GetClientIP(), "", "", "sql_exception", errorMsg);
                }
                throw ex;
            }
        }
Ejemplo n.º 4
0
        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText,
                                           params DbParameter[] commandParameters)
        {
            DbCommand cmd = Provider.CreateCommand();

            try
            {
                using (DbConnection connection = Provider.CreateConnection())
                {
                    connection.ConnectionString = connectionString;
                    PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
                    return(cmd.ExecuteScalar());
                }
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(connectionString))
                {
                    string conStr   = connectionString;
                    string errorMsg = string.Format("{0};{1};{2};{3};{4};{5}", cmdText, commandParameters.GetParamKeyValues(), conStr.Substring(0, Regex.Matches(conStr, ";")[1].Index), ex.Message, ex.Source, ex.StackTrace);
                    Logger.Fatal(errorMsg, ex);
                    DBLog.Process("", "", ClientHelper.GetClientIP(), "", "", "sql_exception", errorMsg);
                }
                throw ex;
            }
            finally
            {
                if (cmd != null && cmd.Parameters != null)
                {
                    cmd.Parameters.Clear();
                }
            }
        }
Ejemplo n.º 5
0
 public static DbDataReader ExecuteReader(DbConnection connection, CommandType cmdType, string cmdText, params DbParameter[] commandParameters)
 {
     try
     {
         DbCommand cmd = Provider.CreateCommand();
         PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
         DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
         cmd.Parameters.Clear();
         return(rdr);
     }
     catch (Exception ex)
     {
         if (connection != null && !string.IsNullOrEmpty(connection.ConnectionString))
         {
             string conStr   = connection == null ? "dbConnection为空" : string.IsNullOrEmpty(connection.ConnectionString) == true ? "ConnectionString为空" : connection.ConnectionString;
             string errorMsg = string.Format("{0};{1};{2};{3};{4};{5}", cmdText, commandParameters.GetParamKeyValues(), connection == null ? conStr : conStr.Substring(0, Regex.Matches(conStr, ";")[1].Index), ex.Message, ex.Source, ex.StackTrace);
             Logger.Fatal(errorMsg, ex);
             DBLog.Process("", "", ClientHelper.GetClientIP(), "", "", "sql_exception", errorMsg);
         }
         throw ex;
     }
 }
Ejemplo n.º 6
0
 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params DbParameter[] commandParameters)
 {
     try
     {
         DbCommand cmd = Provider.CreateCommand();
         using (DbConnection conn = Provider.CreateConnection())
         {
             conn.ConnectionString = connectionString;
             return(ExecuteNonQuery(conn, cmdType, cmdText, commandParameters));
         }
     }
     catch (Exception ex)
     {
         if (!string.IsNullOrEmpty(connectionString))
         {
             string conStr   = connectionString;
             string errorMsg = string.Format("{0};{1};{2};{3};{4};{5}", cmdText, commandParameters.GetParamKeyValues(), conStr.Substring(0, Regex.Matches(conStr, ";")[1].Index), ex.Message, ex.Source, ex.StackTrace);
             Logger.Fatal(errorMsg, ex);
             DBLog.Process("", "", ClientHelper.GetClientIP(), "", "", "sql_exception", errorMsg);
         }
         throw ex;
     }
 }