Exemple #1
0
        private static void PrepareCommand(SqlCommand cmd, SqlTransConn transConn, CommandType cmdType, int?cmdTimeout, string cmdText, SqlParameter[] cmdParms)
        {
            SqlConnection conn = transConn.Conn;

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            cmd.Connection  = conn;
            cmd.CommandText = cmdText;
            if (transConn.Trans != null)
            {
                cmd.Transaction = transConn.Trans;
            }

            cmd.CommandType = cmdType;

            if (cmdTimeout != null)
            {
                cmd.CommandTimeout = cmdTimeout.Value;
            }

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                {
                    cmd.Parameters.Add(parm);
                }
            }
        }
Exemple #2
0
        public static int ExecuteNonQuery(SqlTransConn transConn, CommandType cmdType, int?commandTimeOut, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, transConn, cmdType, commandTimeOut, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();

            cmd.Parameters.Clear();
            return(val);
        }
Exemple #3
0
        public static object ExecuteScalar(SqlTransConn transConn, CommandType cmdType, int?commandTimeOut, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, transConn, cmdType, null, cmdText, commandParameters);
            object val = cmd.ExecuteScalar();

            cmd.Parameters.Clear();
            return(val);
        }
Exemple #4
0
        public static DataTable FillDataTable(SqlTransConn transConn, CommandType cmdType, int?commandTimeOut, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand     cmd = new SqlCommand();
            SqlDataAdapter da  = new SqlDataAdapter();
            DataTable      dt  = new DataTable();

            da.SelectCommand = cmd;
            PrepareCommand(cmd, transConn, cmdType, commandTimeOut, cmdText, commandParameters);

            da.Fill(dt);
            cmd.Parameters.Clear();

            return(dt);
        }
Exemple #5
0
        public static SqlDataReader ExecuteReader(SqlTransConn transConn, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            // we use a try/catch here because if the method throws an exception we want to
            // close the connection throw code, because no datareader will exist, hence the
            // commandBehaviour.CloseConnection will not work
            try
            {
                PrepareCommand(cmd, transConn, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(!transConn.IsInTransaction() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
                cmd.Parameters.Clear();
                return(rdr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #6
0
 private static void PrepareCommand(SqlCommand cmd, SqlTransConn transConn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
 {
     PrepareCommand(cmd, transConn, cmdType, null, cmdText, cmdParms);
 }
Exemple #7
0
 public static object ExecuteScalar(SqlTransConn transConn, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
 {
     return(ExecuteScalar(transConn, cmdType, null, cmdText, commandParameters));
 }
Exemple #8
0
 public static DataTable FillDataTable(SqlTransConn transConn, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
 {
     return(FillDataTable(transConn, cmdType, null, cmdText, commandParameters));
 }
Exemple #9
0
 public static int ExecuteNonQuery(SqlTransConn transConn, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
 {
     return(ExecuteNonQuery(transConn, cmdType, null, cmdText, commandParameters));
 }