自己写的数据库读写类,不足之处请大家多多指教

因为这个是自己没事练手写的,所以呢不太完善请大家见谅。写这个类的目的是希望达到在改动最小的情况下,各个数据库之间的切换,以方便程序的通用性。

 

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

using System.Data.Common;
using System.Data;

namespace Ninja.Classes
{
    public enum DataBaseType
    {
        //System.Data.Odbc、System.Data.OleDb、System.Data.SqlClient、System.Data.SqlServerCe 和 System.Data.OracleClient
        ODBC=0,
        OLEDB,
        SQLCLIENT,
        SQLSERVERCE,
        ORACLECLIENT
    }

    //生成数据库对应的字符串
    public class DataBaseTypeFactory
    {
        public static string GetDataBaseTypeString(DataBaseType dbType)
        {
            string msg = null;
            switch (dbType)
            {
                case DataBaseType.ODBC:
                    msg = "System.Data.Odbc";
                    break;
                case DataBaseType.OLEDB:
                    msg = "System.Data.OleDb";
                    break;
                case DataBaseType.ORACLECLIENT:
                    msg = "System.Data.OracleClient";
                    break;
                case DataBaseType.SQLCLIENT:
                    msg = "System.Data.SqlClient";
                    break;
                case DataBaseType.SQLSERVERCE:
                    msg = "System.Data.SqlServerCe";
                    break;
                default:
                    msg = "System.Data.SqlClient";
                    break;
            }
            return msg;
        }
    }


    public class DataBaseHelper : IDisposable
    {
        private DbDataAdapter adapter;
        private DbConnection connection;
        private DbCommand command;
        private string dataBaseType;


        #region 属性定义
        public string ConnectionString
        {
            get;
            set;
        }
        public string CommandText
        {
            get;
            set;
        }
        public CommandType CmdType
        {
            get;
            set;
        }
        public string DBType
        {
            get
            {
                return dataBaseType;
            }
            set
            {              
                this.dataBaseType = value;
            }
        }
        #endregion


        #region 构造函数定义
        public DataBaseHelper(string dbType, CommandType cmdType)
        {
            this.dataBaseType = dbType;           
            this.ConnectionString = "";
            this.CmdType = cmdType;
            this.CommandText = "";           
        }
        public DataBaseHelper(string dbType)
            : this(dbType, CommandType.Text)
        {

        }

        public DataBaseHelper()
            : this(DataBaseType.SQLCLIENT.ToString())
        {
        }

        #endregion

        private void initializeDB()
        {
            try
            {
                DbProviderFactory factory = DbProviderFactories.GetFactory(this.dataBaseType);
                this.connection = factory.CreateConnection();
                this.command = factory.CreateCommand();
                this.adapter = factory.CreateDataAdapter();
            }
            catch (DbException e)
            {
                throw e;
            }
        }

        public void ConnectAndOpen()
        {
            if (this.ConnectionString == String.Empty)
            {
                throw new ArgumentException("ConnectionString is not allow empty.", "ConnectionString");
            }
            //初始化数据库相关组件     
            initializeDB();
           
            this.connection.ConnectionString = this.ConnectionString;

            try
            {
                this.connection.Open();
            }
            catch (DbException e)
            {
                throw e;
            }
        }

        private void commandInitialize(DbParameter[] parameters)
        {
            this.command.Connection = this.connection;
            this.command.CommandText = this.CommandText;
            this.command.CommandType = this.CmdType;
            if (null != parameters)
            {
                this.command.Parameters.AddRange(parameters);
            }
        }

        public int ExecuteNonQuery(DbParameter[] parameters)       
        {
            commandInitialize(parameters);

            int effectRows = 0;
            try
            {
                effectRows = this.command.ExecuteNonQuery();
            }
            catch(DbException e)
            {
                throw e;
            }

            return effectRows;
        }

        public DataSet Execute(DbParameter[] parameters, string tableName)
        {
            commandInitialize(parameters);
            DataSet ds = new DataSet();

            try
            {
                this.adapter.SelectCommand = this.command;
                this.adapter.Fill(ds, tableName);
            }
            catch (SystemException e)
            {
                throw e;
            }
            return ds;
        }

        public void Close()
        {
            if (this.connection.State == ConnectionState.Open)
                this.connection.Close();
        }

        public void Dispose()
        {           
            if (this.connection.State == ConnectionState.Open)
                this.connection.Close();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值