自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ADO.NET開(kāi)發(fā)數(shù)據(jù)庫(kù)無(wú)關(guān)性應(yīng)用程序

開(kāi)發(fā) 后端
今天我們要講的是ADO.NET開(kāi)發(fā)數(shù)據(jù)庫(kù)無(wú)關(guān)性應(yīng)用程序,數(shù)據(jù)庫(kù)無(wú)關(guān)性(DB Independ),指應(yīng)用程序運(yùn)行不依賴于某一數(shù)據(jù)庫(kù)(如SQL Server),能夠簡(jiǎn)單切換至其它數(shù)據(jù)庫(kù)。

  數(shù)據(jù)庫(kù)無(wú)關(guān)性(DB Independ),指應(yīng)用程序運(yùn)行不依賴于某一數(shù)據(jù)庫(kù)(如 SqlServer),能夠簡(jiǎn)單(不再編譯)切換至其它數(shù)據(jù)庫(kù)(如 Oracle)。數(shù)據(jù)庫(kù)無(wú)關(guān)性是衡量系統(tǒng)的一個(gè)指標(biāo)。

  實(shí)現(xiàn)數(shù)據(jù)庫(kù)無(wú)關(guān)的應(yīng)用程序有很多種方式,可采用 NHibernate、EntityFramework 等。

  本文介紹最基本的一種方式,下面一步步完成一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)無(wú)關(guān)的小程序。

  新建項(xiàng)目

  在Visual Studio中新建 Console Application:

  (VS 2010 可以選擇不同的 .net 版本,如上選擇的是 2.0,也可以選擇其它版本,對(duì)本示例影響不大)

  項(xiàng)目創(chuàng)建完成后,如下添加對(duì) System.Configuration 的引用:

再給項(xiàng)目添加一個(gè)配置文件:

項(xiàng)目準(zhǔn)備完畢,結(jié)構(gòu)如下:

 ?。ú恢罏槭裁矗琒ystem.configuration 中的 configuration 的***個(gè)字母成了小寫(xiě))

  在配置文件中添加連接字符串

  1.   <?xml version="1.0"encoding="utf-8"?> 
  2.   <configuration> 
  3.   <connectionStrings> 
  4.   <add name="default" 
  5. connectionString="server=localhost; 
  6. user id=sa; password=******; database=northwind" 
  7. providerName="System.Data.SqlClient"/>  
  8. connectionString="server=localhost; 
  9. user id=sa; password=******; database=northwind" 
  10.   providerName="System.Data.SqlClient"/>  
  11. connectionString="server=localhost; 
  12. user id=sa; password=******; database=northwind" 
  13.   providerName="System.Data.SqlClient"/> 
  14.   </connectionStrings> 
  15.   </configuration> 

  注意,上面把連接字符寫(xiě)在 connectionStrings 節(jié)中。不可寫(xiě)在 appSettings 中,因?yàn)槌绦蛑幸玫?providerName 這個(gè)屬性。

  編寫(xiě)數(shù)據(jù)庫(kù)無(wú)關(guān)的代碼

  在 Program.cs 中引用如下三個(gè)命名空間:

  1.   using System.Data;  
  2.   usingSystem.Data.Common;  
  3.   usingSystem.Configuration; 

  首先要把配置文件中的連接字符串信息讀出來(lái):

  1. ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["default"]; 

  ADO.NET 的數(shù)據(jù)無(wú)關(guān)使用了工廠模式,根據(jù)配置中的 providerName 可以獲取到這個(gè)工廠:

 

  1. DbProviderFactory factory = DbProviderFactories.GetFactory(settings.ProviderName); 

  DbProviderFactory 提供了多個(gè)方法,幫助我們創(chuàng)建訪問(wèn)數(shù)據(jù)庫(kù)如需的對(duì)象:

  1.    public abstract class DbProviderFactory  
  2.   {  
  3.   //...  
  4.   publicvirtualDbCommand CreateCommand();  
  5.   publicvirtualDbCommandBuilder CreateCommandBuilder();  
  6.   publicvirtualDbConnection CreateConnection();  
  7.   publicvirtualDbConnectionStringBuilder CreateConnectionStringBuilder();  
  8.   publicvirtualDbDataAdapter CreateDataAdapter();  
  9.   publicvirtualDbDataSourceEnumerator CreateDataSourceEnumerator();  
  10.   publicvirtualDbParameter CreateParameter();  
  11.  } 

  DbConnection、DbCommand、DbDataAdapter、DbParameter 等都是數(shù)據(jù)庫(kù)無(wú)關(guān)的。

  下面這幾個(gè)小例子演示了這些對(duì)象的使用:

  1. 查詢員工數(shù)量

  1.   using (DbConnection connection = factory.CreateConnection())  
  2.   {  
  3.   connection.ConnectionString = settings.ConnectionString;  
  4.   DbCommand command = connection.CreateCommand();  
  5.   command.CommandType = CommandType.Text;  
  6.   command.CommandText = "select count(*) from employees";  
  7.   connection.Open();  
  8.   varobj = command.ExecuteScalar();  
  9.   if(obj isint) employeesCount = (int)obj;  
  10.   elseemployeesCount = (long)obj;  
  11.   connection.Close();  
  12.   }  
  13.   Console.WriteLine("共有員工 {0} 人", employeesCount); 

  count(*) 不同數(shù)據(jù)庫(kù)返回值類型不同(SqlServer 返回 Int32,MySql 返回 Int64),10~12行進(jìn)行了處理。

  2. 使用 DbDataReader

  1.   using (DbConnection connection = factory.CreateConnection())  
  2.   {  
  3.   connection.ConnectionString = settings.ConnectionString;  
  4.   DbCommand command = connection.CreateCommand();  
  5.   command.CommandType = CommandType.Text;  
  6.   command.CommandText = "select * from employees";  
  7.   connection.Open();  
  8.   DbDataReader reader = command.ExecuteReader();  
  9.   while(reader.Read())  
  10.   Console.WriteLine("{0} {1}", reader["FirstName"], reader["LastName"]);  
  11.   connection.Close();  
  12.   } 

  3. 填充 Dataset

  1.   DataSet dataSet = newDataSet();  
  2.   using(DbConnection connection = factory.CreateConnection())  
  3.   {  
  4.   connection.ConnectionString = settings.ConnectionString;  
  5.   DbCommand command = connection.CreateCommand();  
  6.   command.CommandType = CommandType.Text;  
  7.   command.CommandText = "select * from employees";  
  8.   DbDataAdapter adapter = factory.CreateDataAdapter();  
  9.   adapter.SelectCommand = command;  
  10.   connection.Open();  
  11.   adapter.Fill(dataSet, "Employees");  
  12.   connection.Close();  
  13.   } 

  切換數(shù)據(jù)庫(kù)

  簡(jiǎn)單修改配置文件,即可換用其它數(shù)據(jù)庫(kù),如下使用 MySql:

  1.   <?xml version="1.0"encoding="utf-8"?>  
  2.   <configuration>  
  3.   <connectionStrings>  
  4.   <add name="default" 
  5. connectionString="server=localhost; user id=root; password=******; database=northwind" 
  6. providerName="MySql.Data.MySqlClient"/>  connectionString="server=localhost; user id=root; password=******; database=northwind" 
  7.   providerName="MySql.Data.MySqlClient"/>  connectionString="server=localhost; user id=root; password=******; database=northwind" 
  8.   providerName="MySql.Data.MySqlClient"/>  
  9.   </connectionStrings>  
  10.  </configuration> 

 ?。ㄒ惭b MySql Connector Net 才能運(yùn)行)

  小結(jié)

  ADO.NET 提供了數(shù)據(jù)庫(kù)無(wú)關(guān)的類,簡(jiǎn)化了數(shù)據(jù)庫(kù)無(wú)關(guān)應(yīng)用程序的開(kāi)發(fā)。

  但開(kāi)發(fā)數(shù)據(jù)庫(kù)無(wú)關(guān)的應(yīng)用程序還有很多要注意的地方,如上面提到的 count(*) 返回值類型問(wèn)題,還有要寫(xiě)數(shù)據(jù)庫(kù)無(wú)關(guān)的 Sql 等等,也非易事。

  ***說(shuō)一下,數(shù)據(jù)庫(kù)無(wú)關(guān)也是有一定限制的,本文介紹的方式只適用于關(guān)系型數(shù)據(jù)。

原文鏈接:http://www.cnblogs.com/ldp615/archive/2011/07/20/develop-db-independ-application-with-ado-dot-net.html

【編輯推薦】

  1. 詳細(xì)述說(shuō)ADO超時(shí)相關(guān)問(wèn)題介紹
  2. 漫談ADO.NET連接池相關(guān)注意問(wèn)題說(shuō)明
  3. 如何更好的進(jìn)行ADO.NET連接池連接
  4. 剖析ADO.NET連接池優(yōu)缺點(diǎn)
  5. 談?wù)凙DO.NET數(shù)據(jù)庫(kù)連接池創(chuàng)建和分配
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2011-10-09 13:38:14

數(shù)據(jù)庫(kù)

2009-12-23 17:30:54

ADO.NET應(yīng)用程序

2009-12-21 13:38:29

ADO.NET應(yīng)用程序

2010-01-04 13:56:50

ADO.NET應(yīng)用程序

2009-10-29 13:16:15

ADO.NET應(yīng)用程序

2009-03-11 14:45:25

Data ServicASP.NETADO.NET

2009-12-23 16:57:35

理解ADO.NET

2009-12-24 14:06:22

ADO.NET 應(yīng)用程

2009-12-23 10:18:21

ADO.NET 應(yīng)用程

2009-12-24 14:12:33

2009-12-18 16:56:05

ADO.NET應(yīng)用程序

2009-12-28 16:50:28

ADO.NET應(yīng)用程序

2011-05-20 13:11:22

ADO.NET

2009-12-22 09:59:06

ADO.NET數(shù)據(jù)庫(kù)

2011-03-04 11:08:46

ADO.NET數(shù)據(jù)庫(kù)

2009-12-31 09:18:23

ADO.NET對(duì)象模型

2009-11-03 14:46:47

ADO.NET數(shù)據(jù)庫(kù)

2009-12-18 17:29:40

ADO.NET應(yīng)用程序

2009-12-25 17:05:32

ADO.NET數(shù)據(jù)庫(kù)

2009-03-19 09:58:04

ADO.NET數(shù)據(jù)庫(kù)SQL操作
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)