首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

自各儿动手写ORM框架(二):AdoHelper支持多数据库操作的封装(1)

2013-02-28 
自己动手写ORM框架(二):AdoHelper支持多数据库操作的封装(1)自己动手写ORM框架系列自己动手写ORM框架(一):

自己动手写ORM框架(二):AdoHelper支持多数据库操作的封装(1)

自己动手写ORM框架系列

自己动手写ORM框架(一):目标效果预览

自己动手写ORM框架(二):AdoHelper支持多数据库操作的封装(1)

在第二章,主要是底层的针对于数据库的CRUD,包括对于不同类型数据库的可扩展性。

第一步:编写AdoHelper类,用于封装对数据库的操作,能同时支持多个数据库(目前支持SqlServer、Oracle、Access),可扩展支持任何类型的数据库。

下面先进行代码片段分析,分析完毕后将贴出完整代码,代码块1-1:

using System;using System.Collections;using System.Collections.Generic;using System.Configuration;using System.Text;using System.Data;using System.Data.Common;using System.Data.SqlClient;using System.Data.OracleClient;using System.Orm.Common;namespace System.Orm.DBUtility{    public class AdoHelper    {        //获取数据库类型        private static string strDbType = CommonUtils.GetConfigValueByKey("dbType").ToUpper();        //将数据库类型转换成枚举类型        public static DatabaseType DbType = DatabaseTypeEnumParse<DatabaseType>(strDbType);
       
       //获取数据库连接字符串        public static string ConnectionString = GetConnectionString("connectionString");       //获取数据库命名参数符号,比如@(SQLSERVER)、:(ORACLE)       public static string DbParmChar = DbFactory.CreateDbParmCharacter();
 

(剖析Step1 Begin)==================================================

    代码块1-1中private static string strDbType = CommonUtils.GetConfigValueByKey("dbType").ToUpper();使用到了CommonUtils.GetConfigValueByKey方法,通过读取传入的dbType字符串,到web.config配置文件中取出所配置的数据库类型,配置文件代码块1-2:

<?xml version="1.0"?><configuration><appSettings>    <add key="dbType" value="oracle"/>    <add key="connectionString" value="DataSource=test;UserID=test;Password=123"/></appSettings><system.web>    <compilation debug="true">    </compilation>    <authentication mode="Windows"/></system.web></configuration>

下面是CommonUtils.GetConfigValueByKey方法的实现代码块1-3:

using System;using System.Collections.Generic;using System.Text;using System.Configuration;using System.Reflection;namespace System.Orm.Common{    public class CommonUtils    {        // <summary>        // 根据传入的Key获取配置文件中的Value值         // </summary>        // <param name="Key"></param>        // <returns></returns>        public static string GetConfigValueByKey(string Key)        {            try            {                return ConfigurationManager.AppSettings[Key].ToString();            }            catch            {                throw new Exception("web.config中 Key=\"" + Key + "\"未配置或配置错误!");            }        }

(剖析Step1 End)==================================================

(剖析Step2 Begin)==================================================

    代码块1-1中public static DatabaseType DbType = DatabaseTypeEnumParse<DatabaseType>(strDbType);

这段代码是将在配置文件中取到的数据库类型字符串(比如:oracle)转换成已经定义的枚举类型,这里转换为DatabaseType枚举,DatabaseType代码块1-4:

using System;using System.Collections.Generic;using System.Text;namespace System.Orm.DBUtility{    // <summary>    // 数据库类型枚举,需要扩展类型可在此添加    // </summary>    public enum DatabaseType    {        SQLSERVER,        ORACLE,        ACCESS,        MYSQL    }}


然后分析如何将字符串转换成枚举类型,用到DatabaseTypeEnumParse这个方法,代码块1-5:

        // <summary>        // 用于数据库类型的字符串枚举转换         // </summary>        // <typeparam name="T"></typeparam>        // <param name="value"></param>        // <returns></returns>        public static T DatabaseTypeEnumParse<T>(string value)        {            try            {                return CommonUtils.EnumParse<T>(value);            }            catch            {                throw new Exception("数据库类型\"" + value + "\"错误,请检查!");            }        }     

这里实现枚举转换的功能来自CommonUtils.EnumParse<T>(value);代码块1-6:

        // <summary>        // 用于字符串和枚举类型的转换         // </summary>        // <typeparam name="T"></typeparam>        // <param name="value"></param>        // <returns></returns>        public static T EnumParse<T>(string value)        {            try            {                return (T)Enum.Parse(typeof(T), value);            }            catch            {                throw new Exception("传入的值与枚举值不匹配。");            }        }

上面代码是泛型的使用,在之前应用中:

DatabaseType DbType = DatabaseTypeEnumParse<DatabaseType(strDbType);  

    传入的DatabaseType枚举是这里的泛型T,传入的strDbType是这里的value,通过web.config中配置,

以及private static string strDbType = CommonUtils.GetConfigValueByKey("dbType").ToUpper();,

我们可以得到strDbType=”ORACLE”;

然后回到代码块1-6中:return (T)Enum.Parse(typeof(T), value);

typeof(T)是得到枚举DatabaseType的类型,value是ORACLE,那么这里可以得到:

return (DatabaseType)Enum.Parse(typeof(DatabaseType), ”ORACLE”);

    这样最后我们可以得到DatabaseType DbType = DatabaseTypeEnumParse<DatabaseType>(strDbType); 中DbType的值为DatabaseType.ORACLE,下面是这里在AdoHelper类中定义的DbType公有成员的用法:

        // <summary>        // 根据配置文件中所配置的数据库类型和传入的         // 数据库链接字符串来创建相应数据库连接对象         // </summary>        // <param name="connectionString"></param>        // <returns></returns>        public static IDbConnection CreateDbConnection(string connectionString)        {            IDbConnection conn = null;            switch (AdoHelper.DbType)            {                case DatabaseType.SQLSERVER:                    conn = new SqlConnection(connectionString);                    break;                case DatabaseType.ORACLE:                    conn = new OracleConnection(connectionString);                    break;                case DatabaseType.ACCESS:                    conn = new OleDbConnection(connectionString);                    break;                default:                     throw new Exception("数据库类型目前不支持!");            }            return conn;        }

(剖析Step2 End)==================================================

(剖析Step3 Begin)==================================================

    代码块1-1中public static string ConnectionString = GetConnectionString("connectionString"); 中通过GetConnectionString(string Key)方法在web.config配置文件中获取数据库连接字符串,配置文件中配置查看代码块1-2,GetConnectionString方法代码块1-7:

        // <summary>        // 根据传入的Key获取配置文件中         // 相应Key的数据库连接字符串         // </summary>        // <param name="Key"></param>        // <returns></returns>        public static string GetConnectionString(string Key)        {            try            {                return CommonUtils.GetConfigValueByKey(Key);            }            catch            {                throw new Exception("web.config文件appSettings中数据库连接字符串未配置或配置错误,必须为Key=\"connectionString\"");            }        }      

 

(剖析Step3 End)==================================================

这里对代码块1-1已经剖析完毕,AdoHelper类中更多的代码剖析将在后面章节中相继完成。

下面是AdoHelper类的完整代码代码如下:

using System;using System.Collections.Generic;using System.Text;namespace System.Orm.Common{    public class TypeUtils    {        public static object ConvertForType(object value,Type type)        {            switch (type.FullName)            {                case "System.String":                    value = value.ToString();                    break;                case "System.Boolean":                    value = bool.Parse(value.ToString());                    break;                case "System.Int16":                case "System.Int32":                case "System.Int64":                    value = int.Parse(value.ToString());                    break;                case "System.Double":                    value = double.Parse(value.ToString());                    break;                case "System.Decimal":                    value = new decimal(double.Parse(value.ToString()));                    break;                                                                         }            return value;        }    }}

数据库操作类代码基本如上全部。

热点排行