数据库访问组件,仿企业库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Blog.Data
{
    /// <summary>
    /// 配置类
    /// </summary>
    public class AppConfig
    {
        /// <summary>
        /// 完整连接字符串
        /// </summary>
        public string ConnectionString
        {
            get;
            set;
        }

        /// <summary>
        /// 完整提供程序
        /// </summary>
        public string ProviderName
        {
            get;
            set;
        }
        /// <summary>
        /// 构造配置对象
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="providerName">提供程序名称</param>
        public AppConfig(string connectionString, string providerName)
        {
            this.ConnectionString = connectionString;
            this.ProviderName = providerName;
        }

        /// <summary>
        /// 构造配置对象
        /// </summary>
        /// <param name="connectionStringName">连接字符串名称</param>
        public AppConfig(string connectionStringName)
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            string providerName = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName;

            this.ConnectionString = connectionString;
            this.ProviderName = providerName;
        }
    }
}



//名称:数据访问组件
//时间:2009年4月9日
//作者:王东升

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Data.ProviderBase;

namespace Blog.Data
{
    /// <summary>
    /// 数据库操作类
    /// </summary>
    public class Database
    {
        #region 定义
        /// <summary>
        /// 数据库连接
        /// </summary>
        private DbConnection Conn = null;

        /// <summary>
        /// 配置对象
        /// </summary>
        private AppConfig config = null;
        /// <summary>
        /// 数据库提供对象
        /// </summary>
        private DbProviderFactory provider = null;
        #endregion

        #region 构造数据库对象
        /// <summary>
        /// 构造数据库对象
        /// </summary>
        /// <param name="connectStringName">连接字符串名称(配置文件中connectionStrings配置节中某连接的Name)</param>
        public Database(string connectStringName)
        {
            config = new AppConfig(connectStringName);

            provider = DbProviderFactories.GetFactory(config.ProviderName);

            Conn = provider.CreateConnection();
            Conn.ConnectionString = config.ConnectionString;
        }
        #endregion

        #region 获取数据库连接对象
        /// <summary>
        /// 获取数据库连接对象
        /// </summary>
        /// <returns>数据库连接对象</returns>
        public DbConnection GetConnection()
        {
            return Conn;
        }
        #endregion

        #region 创建事务
        /// <summary>
        /// 创建事务
        /// </summary>
        /// <returns>事务对象</returns>
        public DbTransaction CreateDbTransaction()
        {
            return this.Conn.BeginTransaction();
        }
        #endregion

        #region 创建数据库命令
        /// <summary>
        /// 创建数据库命令
        /// </summary>
        /// <param name="commandText">命令文本</param>
        /// <returns>命令对象</returns>
        public DbCommand CreateDbCommand(string commandText)
        {
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            DbCommand command = Conn.CreateCommand();
            command.CommandText = commandText;
            command.CommandType = CommandType.Text;
            command.Connection = Conn;
            return command;
        }

        /// <summary>
        /// 创建数据库命令
        /// </summary>
        /// <param name="commandText">命令文本</param>
        /// <param name="type">命令类型</param>
        /// <returns>命令对象</returns>
        public DbCommand CreateDbCommand(string commandText, CommandType type)
        {
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            DbCommand command = Conn.CreateCommand();
            command.CommandText = commandText;
            command.CommandType = type;
            command.Connection = Conn;
            
            return command;
        }
        #endregion

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>值</returns>
        public IDataReader ExecuteReader(DbCommand command)
        {
            try
            {
                return command.ExecuteReader();
            }
            catch (Exception ex)
            {
                command.Connection.Close();
                throw ex;
            }
        }

        #region ExecuteScalar
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>值</returns>
        public object ExecuteScalar(DbCommand command)
        {
            try
            {
                return command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                command.Connection.Close();
                throw ex;
            }
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="commandText">命令文本</param>
        /// <returns>受影响行数</returns>
        public object ExecuteScalar(string commandText)
        {
            try
            {
                DbCommand command = Conn.CreateCommand();
                command.CommandText = commandText;
                command.CommandType = CommandType.Text;
                command.Connection.Close();
                return command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region ExecuteNonQuery
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>受影响行数</returns>
        public int ExecuteNonQuery(DbCommand command)
        {
            try
            {
                return command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="Transaction">事务</param>
        /// <returns>受影响行数</returns>
        public int ExecuteNonQuery(DbCommand command, DbTransaction Transaction)
        {
            try
            {
                command.Transaction = Transaction;

                int row = command.ExecuteNonQuery();
                Transaction.Commit();
                return row;
            }
            catch (Exception ex)
            {
                Transaction.Rollback();
                throw ex;
            }
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="commandText">命令文本</param>
        /// <returns>受影响行数</returns>
        public int ExecuteNonQuery(string commandText, DbTransaction Transaction)
        {
            try
            {
                DbCommand command = CreateDbCommand(commandText);

                int row = command.ExecuteNonQuery();
                Transaction.Commit();
                return row;
            }
            catch (Exception ex)
            {
                Transaction.Rollback();
                throw ex;
            }
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="commandText">命令文本</param>
        /// <returns>受影响行数</returns>
        public int ExecuteNonQuery(string commandText)
        {
            try
            {
                DbCommand command = Conn.CreateCommand();
                command.CommandText = commandText;
                command.CommandType = CommandType.Text;

                return command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region ExecuteDataSet
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>值</returns>
        public DataSet ExecuteDataSet(DbCommand command)
        {
            try
            {
                DataSet ds = new DataSet();
                DbDataAdapter adp = provider.CreateDataAdapter();
                adp.SelectCommand = command;
                adp.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="commandText">命令</param>
        /// <returns>值</returns>
        public DataSet ExecuteDataSet(string commandText)
        {
            try
            {
                DbCommand command = CreateDbCommand(commandText);

                DataSet ds = new DataSet();
                DbDataAdapter adp = provider.CreateDataAdapter();
                adp.SelectCommand = command;
                adp.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取数据集(分页)
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="tableName">表名</param>
        /// <param name="strwhere">条件</param>
        /// <param name="orderby">排序</param>
        /// <param name="page">数据页</param>
        /// <param name="pagesize">页大小</param>
        /// <returns>数据集</returns>
        public DataSet ExecuteDataSet(DbCommand command, string tableName, string strwhere, string orderby, int page, int pagesize)
        {
            try
            {
                DataSet ds = new DataSet();
                DbDataAdapter adp = provider.CreateDataAdapter();
                adp.SelectCommand = command;
                adp.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 参数
        /// <summary>
        /// 新增参数
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="paramName">参数名</param>
        /// <param name="type">参数类型</param>
        /// <param name="value">参数值</param>
        public void AddParameter(DbCommand command, string parameterName, DbType type, object value)
        {
            DbParameter param = command.CreateParameter();
            param.DbType = type;
            param.Direction = ParameterDirection.Input;
            param.ParameterName = parameterName;
            param.Value = value;

            command.Parameters.Add(param);
        }

        /// <summary>
        /// 新增参数
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="paramName">参数名</param>
        /// <param name="type">参数类型</param>
        /// <param name="value">参数值</param>
        public void AddParameter(DbCommand command, string parameterName, DbType type, object value, ParameterDirection direction)
        {
            DbParameter param = command.CreateParameter();
            param.DbType = type;
            param.Direction = direction;
            param.ParameterName = parameterName;
            param.Value = value;

            command.Parameters.Add(param);
        }
        #endregion
    }
}

 

posted @ 2010-02-04 15:38  部落格  阅读(994)  评论(2编辑  收藏  举报