目录:
“Zhuang.Data”轻型数据库访问框架(二)框架的入口DbAccessor对象
先来看一段代码
DbAccessor dba = DbAccessor.Create(); var dt = dba.QueryDataTable("select * from sys_product where productid=#Id#",new {Id=1}); Console.WriteLine(DataTableUtil.ToString(dt));
实际执行的sql
exec sp_executesql N‘select * from sys_product where [email protected]‘,N‘@Id int‘,@Id=1
DbAccessor抽象类
DbAccessor是一个抽象类(不同数据库会对应具体的实现类,如:SqlServerAccessor、OracleAccessor和MySqlAccessor),该类封装了许多用来执行sql的方法,如:Execute、ExecuteReader、ExecuteScalar、QueryDataSet、QueryDataTable和QueryEntities等方法。也就是说只要有了该抽象类的一个对象就可以使用以上的方法,那么如何能得到一个DbAccessor对象呢?DbAccessorFactory这个工厂类就是专门用来创建DbAccessor对象的,其实上面代码上的DbAccessor.Create()方法也是去调用DbAccessorFactory.Create()去得一个DbAccessor对象的。
DbAccessorFactory工厂类
public static DbAccessor NewDbAccessor(string connectionString, string providerName) { DbAccessor dba = null; if (string.IsNullOrEmpty(providerName) || providerName == "System.Data.SqlClient" || providerName.ToLower() == DbProviderName.SqlServer.ToString().ToLower()) { dba = new SqlServerAccessor(connectionString); EvnValService.SetDbAccessorDbProviderName(dba, DbProviderName.SqlServer); } else if (providerName.ToLower() == DbProviderName.Oracle.ToString().ToLower()) { dba = new OracleAccessor(connectionString); EvnValService.SetDbAccessorDbProviderName(dba, DbProviderName.Oracle); } else if (providerName.ToLower() == DbProviderName.MySql.ToString().ToLower()) { dba = new MySqlAccessor(connectionString); EvnValService.SetDbAccessorDbProviderName(dba, DbProviderName.MySql); } else { Type tProviderName = Type.GetType(providerName); if (tProviderName == null) { throw new Exception(string.Format("ConnectionString({0})的ProviderName({1})找不到该类型!", connectionString, providerName)); } else if (!(tProviderName.IsSubclassOf(typeof(DbAccessor)))) { throw new Exception(string.Format("ConnectionString({0})的ProviderName({1})该类型不是DbAccessor的实现类!", connectionString, providerName)); } object oProviderName = Activator.CreateInstance(tProviderName, connectionString); dba = oProviderName as DbAccessor; } return dba; }
以上代码DbAccessorFactory类中一个方法,方法所做的就是,根据App.config或Web.config中配置中connectionString配置去创建具体的DbAccessor实现类
<connectionStrings> <add name="DefaultDb" connectionString="Data Source=127.0.0.1;Initial Catalog=zhuangdb;Persist Security Info=True;User ID=sa; PassWord=zwb" providerName="sqlserver"/> <add name="MySqlDb" connectionString="Data Source=192.168.121.130;Initial Catalog=zhuangdb;Persist Security Info=True;User ID=root; PassWord=zwb" providerName="mysql"/> <add name="OracleDb" connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.126.129)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xe)));User Id=zhuangdb;Password=zwb;" providerName="oracle"/> </connectionStrings>
connectionString配置中有一个providerName的属性,DbAccessorFactory会根据这里配置是什么数据库provider去创建具体对应的数据库DbAccessor实现类,如:当
providerName="sqlserver"时工厂创建出来的是一个SqlServerAccessor类的实例。
DbAccessorFactory类常用方法介绍
1、GetDbAccessor()
得到一个DbAccessor单例对象;
2、CreateDbAccessor()
创建一个新的DbAccessor对象,当前需要使用事务时候需要创建新的对象而不能使用单例;
3、CreateDbAccessor(string name)
创建一个新的DbAccessor对象,参数“name”对应配置文件中connectionStrings配置中的项置项名称(如果没有指定name的值的话将会自动取一个默认值“DefaultDb”);
未完,待续……