目錄:
“Zhuang.Data”輕型數據庫訪問框架(一)開篇介紹
“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 productid=@Id',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”);
未完,待續……