首页 > 代码库 > 数据库解耦源码

数据库解耦源码

创建数据库接口:

namespace DataModels.IDBHandler{    /// <summary>    /// 配置参数 数据库操作基类    /// </summary>    public interface IDBCreater    {        int Createtable();    }}

DBBase 类:

using System.Configuration;using ToolsPakage;namespace DataModels.IDBHandler{    public class DBBase    {        public static string ConnString = Security.DecryptDES(ConfigurationManager.AppSettings["dbConn"]);    }}

SqliteCreater:

public class SqliteCreater : IDBCreater    {        DbUtility utility;        string path = AppDomain.CurrentDomain.BaseDirectory + "Alarm.db";        string source = @"data source={0};version=3";        public SqliteCreater()        {            DBBase.ConnString = string.Format(source,path);            utility = new DbUtility();        }        public int Createtable()        {            if(File.Exists(path))            {                return 0;            }            try            {                if (!CreateTableConfigINFO())                {                    return -1;                }                if (!CreateTableCompanyInfo())                {                    return -1;                }                if (!CreateTableUserInfo())                {                    return -1;                }                if (!CreateTableLog())                {                    return -1;                }                if (!CreateTableOpratorFunction())                {                    return -1;                }                if (!CreateTableFacility())                {                    return -1;                }                if (!CreateTableContacts())                {                    return -1;                }                if (!InsertAdmin())                {                    return -1;                }                if(!CreateTableGroup())                {                    return -1;                }                return 1;            }            catch            {                return -1;            }        }    }


DbUtility 类包含一下代码以及一些基本数据库操作代码(通用):

public string ConnectionString { get; set; }        private DbProviderFactory providerFactory;        /// <summary>        /// 构造函数        /// </summary>        /// <param name="connectionString">数据库连接字符串</param>        /// <param name="providerType">数据库类型枚举,参见<paramref name="providerType"/></param>        public DbUtility()        {            ConnectionString = DBBase.ConnString;            string privoder = System.Configuration.ConfigurationManager.AppSettings["dbPrivoder"];            DbProviderType type = (DbProviderType)Enum.Parse(typeof(DbProviderType), privoder);            providerFactory = ProviderFactory.GetDbProviderFactory(type);            if (providerFactory == null)            {                throw new ArgumentException("Can‘t load DbProviderFactory for given value of providerType");            }        }

SqliteProvider:

public class SQLitePrivoder : DbProviderFactory    {        public override DbCommand CreateCommand()        {            DbCommand command = new SQLiteCommand();            return command;        }        public override DbConnection CreateConnection()        {            DbConnection conn = new SQLiteConnection();            return conn;        }        public override DbDataAdapter CreateDataAdapter()        {            DbDataAdapter ada = new SQLiteDataAdapter();            return ada;        }        public override DbParameter CreateParameter()        {            DbParameter para = new SQLiteParameter();            return para;        }    }

业务逻辑操作接口:

public interface ILogHandler    {        bool Add(TabLog model);        bool Update(TabLog model);        bool Delete(long ID);        bool DeleteList(string IDlist, out int count);        TabLog GetModel(long ID);        TabLog DataRowToModel(DataRow row);        DataTable GetLogDataSetByWhere(string where);        DataTable GetDataTableByPage(int pageSize, int pageIndex, out int pageCount);    }

业务逻辑实现:

public class TabLogHandler : ILogHandler    {        DbUtility utility;        string path = AppDomain.CurrentDomain.BaseDirectory + "Alarm.db";        string source = @"data source={0};version=3";        public TabLogHandler()        {            DBBase.ConnString = string.Format(source, path);            utility = new DbUtility();        }

业务逻辑接口工厂:

/// <summary>    /// 返回抽象处理接口    /// </summary>    public class HandlerFactory    {        static string privoder = System.Configuration.ConfigurationManager.AppSettings["dbPrivoder"];        private static Dictionary<DbProviderType, IComInfoHandler> _comInfoDic = new Dictionary<DbProviderType, IComInfoHandler>();        private static Dictionary<DbProviderType, IContractsHandler> _contractDic = new Dictionary<DbProviderType, IContractsHandler>();        private static Dictionary<DbProviderType, IFacilityHandler> _facilityDic = new Dictionary<DbProviderType, IFacilityHandler>();        private static Dictionary<DbProviderType, ILogHandler> _logDic = new Dictionary<DbProviderType, ILogHandler>();        private static Dictionary<DbProviderType, IOperatorFunctionHandler> _operatorFunDic = new Dictionary<DbProviderType, IOperatorFunctionHandler>();        private static Dictionary<DbProviderType, IOperatorHandler> _operatorDic = new Dictionary<DbProviderType, IOperatorHandler>();        private static Dictionary<DbProviderType, IUserInfoHandler> _userInfoDic = new Dictionary<DbProviderType, IUserInfoHandler>();        private static Dictionary<DbProviderType, IGroupHandler> _groupDic = new Dictionary<DbProviderType, IGroupHandler>();        private static Dictionary<DbProviderType, IDBCreater> _dbCreateDic = new Dictionary<DbProviderType, IDBCreater>();        private HandlerFactory()        {                    }        public static IComInfoHandler CreateComInfoHandler()        {            try            {                return (IComInfoHandler)_comInfoDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static IContractsHandler CreateContractsHandler()        {            try            {                return (IContractsHandler)_contractDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static IFacilityHandler CreateFacilityHandler()        {            try            {                return (IFacilityHandler)_facilityDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static ILogHandler CreateLogHandler()        {            try            {                return (ILogHandler)_logDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static IOperatorFunctionHandler CreateOperatorFunctionHandler()        {            try            {                return (IOperatorFunctionHandler)_operatorFunDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static IOperatorHandler CreateOperatorHandler()        {            try            {                return (IOperatorHandler)_operatorDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static IUserInfoHandler CreateUserInfoHandler()        {            try            {                return (IUserInfoHandler)_userInfoDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static IGroupHandler CreateGroupHandler()        {            try            {                return (IGroupHandler)_groupDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static IDBCreater CreateDBbyType()        {            try            {                return (IDBCreater)_dbCreateDic[(DbProviderType)Enum.Parse(typeof(DbProviderType), privoder)];            }            catch (Exception ex)            {                throw ex;            }        }        public static bool InitDBPrivoder()        {            try            {                #region Sqlite 映射                _comInfoDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabComInfoHandler());                _contractDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabContactsHandler());                _facilityDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabFacilityHandler());                _logDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabLogHandler());                _operatorFunDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabOperatorFunctionHandler());                _operatorDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabOperatorHandler());                _userInfoDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabUserInfoHandler());                _dbCreateDic.Add(DbProviderType.SQLite, new DataModels.DBCreater.SqliteCreater());                _groupDic.Add(DbProviderType.SQLite, new DataModels.SqliteHandler.TabGroupHandler());                #endregion                #region Mysql 映射                _comInfoDic.Add(DbProviderType.MySql, new DataModels.MySqlHandler.TabComInfoHandler());                _contractDic.Add(DbProviderType.MySql, new DataModels.MySqlHandler.TabContactsHandler());                _facilityDic.Add(DbProviderType.MySql, new DataModels.MySqlHandler.TabFacilityHandler());                _logDic.Add(DbProviderType.MySql, new DataModels.MySqlHandler.TabLogHandler());                _operatorFunDic.Add(DbProviderType.MySql, new DataModels.MySqlHandler.TabOperatorFunctionHandler());                _operatorDic.Add(DbProviderType.MySql, new DataModels.MySqlHandler.TabOperatorHandler());                _userInfoDic.Add(DbProviderType.MySql, new DataModels.MySqlHandler.TabUserInfoHandler());                _dbCreateDic.Add(DbProviderType.MySql, new DataModels.DBCreater.MySqlCreater());                #endregion                return true;            }            catch            {                return false;            }        }    }}


配置文件:

数据库解耦源码