SqlHelper.cs

  

    public static class SqlHelper
    {
        public static object FromDbValue(object value)
        {
            if (value == DBNull.Value)
            {
                return null;
            }
            else
            {
                return value;
            }
           
        }

        public static object ToDbValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        } 

        private static string connStr = ConfigurationManager.ConnectionStrings["myconnstring"].ConnectionString;

        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }

        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(cmd);
                    apdater.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
        public static DataSet ExecuteDataSet(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(cmd);
                    apdater.Fill(dataset);
                    return dataset;
                }
            }
        }
    }
View Code

 

2016.1.29

1.增加存储过程

2.修改垃圾回收机制

    public class SQL_Helper
    {
        private static readonly string connectionString =
            ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

        public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteNonQuery();
                }
            }
        }
        public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteScalar();
                }
            }
        }
        public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters) {
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                using( SqlCommand command = new SqlCommand( cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(command);
                    apdater.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
        public static DataSet ExecuteDataSet(string cmdText,params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                using (SqlCommand command = new SqlCommand(cmdText, connection)) {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    DataSet dataset = new DataSet();
                    SqlDataAdapter sda = new SqlDataAdapter(command);
                    sda.Fill(dataset);
                    return dataset;
                }
            }
        }
        public static SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                        return command.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
        }
        public static SqlDataReader ExecuteReaderSp(string cmdText, params SqlParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    command.CommandType = CommandType.StoredProcedure;
                    return command.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
        }
    }
View Code

 2016.1.30

1.修改存储过程,返回 object dataset  dataread datatable

    public class SqlHelper
    {


        #region comm sql
        public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteNonQuery();
                }
            }
        }
        public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteScalar();
                }
            }
        }
        public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(command);
                    apdater.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
        public static DataSet ExecuteDataSet(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    DataSet dataset = new DataSet();
                    SqlDataAdapter sda = new SqlDataAdapter(command);
                    sda.Fill(dataset);
                    return dataset;
                }
            }
        }
        public static SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteReader();
                }
            }
        }
        #endregion

        #region storedProcedure 
        public static int ExecuteNonQuerySp(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    command.CommandType = CommandType.StoredProcedure;
                    return command.ExecuteNonQuery();
                }
            }
        }
        public static object ExecuteScalarSp(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    command.CommandType = CommandType.StoredProcedure;
                    return command.ExecuteScalar();
                }
            }
        }
        public static DataTable ExecuteDataTableSp(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    command.CommandType = CommandType.StoredProcedure;
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(command);                    
                    apdater.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
        public static SqlDataReader ExecuteReaderSp(string cmdText, params SqlParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    command.CommandType = CommandType.StoredProcedure;
                    return command.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
        }
        public static DataSet ExecuteDataSetSp(string cmdText, params SqlParameter[] parameters) {
            using (SqlConnection conn = new SqlConnection(connectionString)) {
                using (SqlCommand comm = new SqlCommand(cmdText, conn)) {
                    if (parameters != null) comm.Parameters.AddRange(parameters);
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    comm.CommandType = CommandType.StoredProcedure;
                    DataSet ds = new DataSet();
                    SqlDataAdapter sda = new SqlDataAdapter(comm);
                    sda.Fill(ds);
                    return ds;
                }
            }
        }

        #endregion
    }
View Code

 2016.4.19

1.增加事务管理

    public class MSSQLHelper
    {

        private static string connectionString = ConfigurationManager.ConnectionStrings["myconnstring"].ConnectionString;
        #region comm sql
        public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteNonQuery();
                }
            }
        }
        public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteScalar();
                }
            }
        }
        public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(command);
                    apdater.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
        public static DataSet ExecuteDataSet(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    DataSet dataset = new DataSet();
                    SqlDataAdapter sda = new SqlDataAdapter(command);
                    sda.Fill(dataset);
                    return dataset;
                }
            }
        }
        public static SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteReader();
                }
            }
        }
        #endregion

        #region storedProcedure 
        public static int ExecuteNonQuerySp(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();

                    return command.ExecuteNonQuery();
                }
            }
        }
        public static object ExecuteScalarSp(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteScalar();
                }
            }
        }
        public static DataTable ExecuteDataTableSp(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    DataSet dataset = new DataSet();
                    SqlDataAdapter apdater = new SqlDataAdapter(command);
                    apdater.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
        public static SqlDataReader ExecuteReaderSp(string cmdText, params SqlParameter[] parameters)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            {
                using (SqlCommand command = new SqlCommand(cmdText, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    if (parameters != null) command.Parameters.AddRange(parameters);
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    return command.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
        }
        public static DataSet ExecuteDataSetSp(string cmdText, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand comm = new SqlCommand(cmdText, conn))
                {
                    if (parameters != null) comm.Parameters.AddRange(parameters);
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    comm.CommandType = CommandType.StoredProcedure;
                    DataSet ds = new DataSet();
                    SqlDataAdapter sda = new SqlDataAdapter(comm);
                    sda.Fill(ds);
                    return ds;
                }
            }
        }

        #endregion

        #region transaction
        public static bool RunSqlsTran(string[] strSql)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand comm = new SqlCommand())
                {
                    int len = strSql.Length;
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    SqlTransaction myTrans = conn.BeginTransaction();
                    try
                    {
                        comm.Connection = conn;
                        comm.Transaction = myTrans;
                        foreach (var s in strSql)
                        {
                            comm.CommandText = s;
                            comm.ExecuteNonQuery();
                        }
                        myTrans.Commit();
                        return true;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        myTrans.Rollback();
                        return false;
                    }
                }
            }
        }
        public static bool RunSqlsTran(string[] strSql , SqlParameter[] parameter)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand comm = new SqlCommand())
                {
                    int len = strSql.Length;
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    SqlTransaction myTrans = conn.BeginTransaction();
                    try
                    {
                        int strLen = strSql.Length;
                        comm.Connection = conn;
                        comm.Transaction = myTrans;
                        for(int i = 0;i < strLen; i++)
                        {
                            comm.CommandText = strSql[i];
                            comm.Parameters.Add(parameter[i]);
                            comm.ExecuteNonQuery();
                        }
                    
                        myTrans.Commit();
                        return true;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        myTrans.Rollback();
                        return false;
                    }
                }
            }
        }
        #endregion
    }
View Code

 20160621  看到一个好的代码,重写ado.net的集中方式,编码普通和存储过程的重复代码

 

    public abstract class SQLHelper
    {
        public static readonly string txtConnecttionString = ConfigurationManager.ConnectionStrings["SqlConnectStringOne"].ConnectionString;


        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
        {

            SqlCommand cmd = new SqlCommand();
            using (SqlConnection con = new SqlConnection(txtConnecttionString))
            {

                PrepareCommand(cmd, con, null, cmdType, cmdText, cmdParas);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;

            }


        }
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
        {

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand();
            try
            {

                PrepareCommand(cmd, con, null, cmdType, cmdText, cmdParas);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;

            }
            catch (SqlException ex)
            {

                con.Close();
                throw new Exception(ex.Message, ex);

            }
        }
        public static object ExecuteSclare(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
        {

            SqlCommand cmd = new SqlCommand();
            using (SqlConnection con = new SqlConnection(connectionString))
            {

                PrepareCommand(cmd, con, null, cmdType, cmdText, cmdParas);

                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;

            }

        }
        public static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = cmdType;

            //当定义cmdParas.Length< 0的时候,在调用该方法时,如果参数为空的话就会报错,错误为“调用的对象可能为空”,所以使用cmdParas!=null

            if (cmdParas != null)
            {

                foreach (SqlParameter para in cmdParas)
                {

                    cmd.Parameters.Add(para);

                }

            }

        }
    }
}
View Code

 

posted @ 2016-01-22 09:20  少时不知贵  阅读(193)  评论(0编辑  收藏  举报