"Zhuang. Data" lightweight database access framework (ii) Portal DbAccessor object of the Framework, lightweight Database

Source: Internet
Author: User
Tags connectionstrings

"Zhuang. Data" lightweight database access framework (ii) Portal DbAccessor object of the Framework, lightweight Database

Directory:

Introduction to "Zhuang. Data" lightweight database access framework (I)

"Zhuang. Data" access framework for lightweight databases (ii) Portal DbAccessor object of the framework

 

 

Let's take a look at a piece of code.

DbAccessor dba = DbAccessor.Create();var dt = dba.QueryDataTable("select * from sys_product where productid=#Id#",new {Id=1});Console.WriteLine(DataTableUtil.ToString(dt));

Actually executed SQL

exec sp_executesql N'select * from sys_product where productid=@Id',N'@Id int',@Id=1

DbAccessor abstract class

DbAccessor is an abstract class (different databases correspond to specific implementation classes, such as SqlServerAccessor, OracleAccessor, and MySqlAccessor). This class encapsulates many SQL Execution methods, such: execute, ExecuteReader, ExecuteScalar, QueryDataSet, QueryDataTable, and QueryEntities. That is to say, as long as an object of this abstract class can use the above method, how can we get a DbAccessor object? The factory class DbAccessorFactory is used to Create the DbAccessor object. In fact, the DbAccessor. Create () method in the code above also calls DbAccessorFactory. Create () to get a DbAccessor object.

DbAccessorFactory factory class

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 tPr OviderName = Type. getType (providerName); if (tProviderName = null) {throw new Exception (string. format ("provionstring ({0}) ProviderName ({1}) cannot find this type! ", ConnectionString, providerName);} else if (! (TProviderName. isSubclassOf (typeof (DbAccessor) {throw new Exception (string. format ("provionstring ({0}) ProviderName ({1}) This type is not the Implementation class of DbAccessor! ", ConnectionString, providerName);} object oProviderName = Activator. CreateInstance (tProviderName, connectionString); dba = oProviderName as DbAccessor;} return dba ;}

In the preceding code, a method in the DbAccessorFactory class is used to create a specific DbAccessor implementation Class Based on the connectionString configuration in App. config or Web. config.

  <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 configuration has a providerName attribute. DbAccessorFactory will create a specific database DbAccessor implementation class based on the database provider configured here. For example: When

When providerName = "sqlserver", the factory creates an instance of the SqlServerAccessor class.

DbAccessorFactory

1. GetDbAccessor ()

Obtain a DbAccessor singleton object;

2. CreateDbAccessor ()

Create a new DbAccessor object. To use transactions, you must create a new object instead of a singleton object;

3. CreateDbAccessor (string name)

Create a new DbAccessor object, the parameter "name" corresponds to the configuration item name in the connectionStrings configuration in the configuration file (if the value of name is not specified, a default value "DefaultDb" will be automatically selected ");

 

Unfinished, To be continued ......

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.