ADO.NET 封装的数据库帮助类

using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace DAL
{
    /// 
    /// 数据库帮助类
    /// 
    public abstract class DatabaseHelper
    {
        # region 成员变量

        /// 
        /// 数据库连接字符串
        /// 
        private static string m_ConnectionString = null;

        # endregion

        # region 属性

        /// 
        /// 获取数据库连接字符串
        /// 
        /// 
        public string ConnectionString
        {
            get
            {
                if (m_ConnectionString == null)
                {
                    m_ConnectionString = ReadConnectionString("ConnectionString");
                }

                return m_ConnectionString;
            }
        }

        # endregion

        # region 构造函数

        public DatabaseHelper()
        {
            m_ConnectionString = ReadConnectionString("ConnectionString");
        }

        public DatabaseHelper(string connectionString)
        {
            m_ConnectionString = connectionString;
        }

        # endregion

        # region 静态方法

        /// 
        /// 从配置文件读取数据库连接字符串
        /// 
        /// 
        /// 
        private static string ReadConnectionString(string configKeyName)
        {
            string connectionString = "";

            object obj = ConfigurationManager.ConnectionStrings[configKeyName];
            if (obj != null)
            {
                connectionString = obj.ToString();
            }

            return connectionString;
        }

        /// 
        /// 获取数据库连接字符串
        /// 
        /// 
        public static string GetConnectionString()
        {
            if (m_ConnectionString == null)
            {
                m_ConnectionString = ReadConnectionString("ConnectionString");
            }

            return m_ConnectionString;
        }

        #endregion

        #region 抽象方法

        protected abstract object DataReaderToEntity(IDataReader dataReader);

        #endregion

        #region 数据库操作方法

        /// 
        /// 构造数据库操作命令
        /// 
        /// 数据库连接对象
        /// sql语句
        private SqlCommand CreateCommand(SqlConnection conn, string cmdString)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = cmdString;

            return cmd;
        }

        /// 
        /// 清空参数,释放SqlCommand资源
        /// 
        /// 
        private void DisposeCommand(SqlCommand cmd)
        {
            if (cmd != null)
            {
                cmd.Parameters.Clear();
                cmd.Dispose();
            }
        }

        /// 
        /// 绑定SqlCommand参数,参数值为空时不添加
        /// 
        /// 
        /// 
        private void AttachParameters(SqlCommand cmd, params SqlParameter[] parameters)
        {
            if (parameters != null)
            {
                foreach (SqlParameter para in parameters)
                {
                    if (para != null)
                    {
                        if (para.Direction == ParameterDirection.InputOutput ||
                            (para.Direction == ParameterDirection.Input && para.Value == null))
                        {
                            para.Value = DBNull.Value;
                        }
                        cmd.Parameters.Add(para);
                    }
                }
            }
        }

        /// 
        /// 执行一个查询,返回查询的结果集
        /// 
        /// sql语句
        /// 参数化参数
        /// 查询的结果集
        public DataTable ExecuteDataTable(string sqlString, params SqlParameter[] parameters)
        {
            DataTable dataTable = new DataTable();

            using (SqlConnection conn = new SqlConnection(m_ConnectionString))
            {
                conn.Open();

                SqlCommand cmd = CreateCommand(conn, sqlString);
                AttachParameters(cmd, parameters);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dataTable);

                DisposeCommand(cmd);
            }

            return dataTable;
        }

        /// 
        /// 执行sql操作,返回受影响的行数
        /// 
        /// sql语句
        /// 参数化参数
        /// 受影响的行数
        public int ExecuteNonQuery(string sqlString, params SqlParameter[] parameters)
        {
            int num = 0;

            using (SqlConnection conn = new SqlConnection(m_ConnectionString))
            {
                conn.Open();

                SqlCommand cmd = CreateCommand(conn, sqlString);
                AttachParameters(cmd, parameters);
                num = cmd.ExecuteNonQuery();
                DisposeCommand(cmd);
            }

            return num;
        }

        /// 
        /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
        /// 
        /// sql语句
        /// 参数化参数
        /// 结果集中第一行的第一列;如果结果集为空,则为空引用
        public object ExecuteScalar(string sqlString, params SqlParameter[] parameters)
        {
            object obj = new object();

            using (SqlConnection conn = new SqlConnection(m_ConnectionString))
            {
                conn.Open();

                SqlCommand cmd = CreateCommand(conn, sqlString);
                AttachParameters(cmd, parameters);
                obj = cmd.ExecuteScalar();
                DisposeCommand(cmd);
            }

            return obj;
        }

        public SqlDataReader ExecuteReader(string sqlString, params SqlParameter[] parameters)
        {
            SqlDataReader reader = null;

            SqlConnection conn = new SqlConnection(m_ConnectionString);
            conn.Open();
            SqlCommand cmd = CreateCommand(conn, sqlString);
            AttachParameters(cmd, parameters);
            reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            DisposeCommand(cmd);

            return reader;
        }

        # endregion
    }
}

你可能感兴趣的:(C#,ado-net)