写一个通用数据访问组件

** 写一个通用数据访问组件 ** ** **

Willsound( [email protected] )

我收到过好多 Email 来问我如何用一个通用的数据提供者 ( data provider) 在不失自然数据提供者 (native data provider) 稳定而强大功能的前提下来访问不同的数据源 (data sources). 一个小伙子甚至问我能不能写一些代码在程序运行时指定 数据提供者 ( data provider) 。

** 绪论 ** ** : **

ADO.net 对于不同的数据源提供了不同的数据提供者,三个通用的数据提供者分别是 OLE DB, SQL, and ODBC 。使用不同数据提供者的用意就在于可以针对不同的数据源提供最强大且稳定的数据访问技术。例如,当你访问 Access 数据库时采用 OLE DB data provider 这是最有效的方法,但是如果你采用 ODBC data provider 时,它是建在 OLE DB data provider 基础上的,所以效率就会打折扣。

实际上,所有的数据提供者类 (data provider classes), 比如连接 (connection), 命令 (command), 数据适配器 (data adapter) 和数据读取者 (data reader) 都是从某一特定接口继承的。我希望写篇文章深入的讨论这些,但这要花费我很多天的时间。

总之,我的这篇文章的主要问题是如何写一个通用的类能够在运行时根据用户的选择而分别采用 OLE DB, SQL, and ODBC data providers 访问数据源。

** 接口模型 ** ** : **

每一种数据提供者都实现了一些接口 (interfaces) ,这些接口都定义在 System.Data 名字空间 (namespace) 里面。例如 SqlConnection, OleDbConnection, and OdbcConnection 类就是从 IdbConnection 接口继承下来的。类似于 connection 类,其它的 ADO.net 组件像 DataAdapter, DataReader, Command 也都是从某个接口继承下来的。

你们就将使用这些接口来实现通用数据访问类。我不打算去写所有的这些功能,但我会给你们如何扩展这些功能提供一个好的想法。

下面的代码 1 展现了一个 GenericAdoNetComp 类 , 它提供了二个方法, GetConnection 和 GetDataAdapter 。这两个方法都是从用户提供提供的信息关基于 connection 读取信息,这二个方法将返回所希望的输出。下面就是这两个方法的定义

** public IDbConnection GetConnection(int connType,
string connString) **

** public IDbDataAdapter GetDataAdapter(int connType,
string connString, string sql) **

正如你你所见,我们用 IdBConnection 取代 connection 和一个数据提供者相连 , 方法将返回 IdbConnection. 从下面的代码 1 中你们将看到我们根据用户在运行时提供的连结种类类型参数 ( connection type argument) 来生成 SqlConnection, OleDbConnection, or 或者 OdbcConnection 。

// 代码 1

using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using Microsoft.Data.Odbc;

namespace GenericDataAccessApp
{
public class GenericAdoNetComp
{
private IDbConnection idbConn = null;
private IDbDataAdapter idbAdapter = null;
private DbDataAdapter dbAdapter = null;
private IDataReader iReader = null;

public GenericAdoNetComp()
{
}

// GetConnection returns IDbConnection
**public IDbConnection GetConnection(int connType,
string connString) **
{
switch(connType)
{
case 1: // OleDb Data Provider
idbConn = new OleDbConnection(connString);
break;
case 2: // Sql Data Provider
idbConn = new SqlConnection(connString);
break;
case 3: // ODBC Data Provider
idbConn = new OdbcConnection(connString);
break;
// case 3: // Add your custom data provider
default:
break;
}
return idbConn;
}

// GetDataAdapter returns IDbDataAdapter
**public IDbDataAdapter GetDataAdapter(int connType,
string connString, string sql) **
{
switch(connType)
{
case 1: // OleDb Data Provider
idbAdapter = new OleDbDataAdapter(sql, connString);
break;
case 2: // Sql Data Provider
idbAdapter = new SqlDataAdapter(sql, connString);
break;
case 3: // ODBC Data Provider
idbAdapter = new OdbcDataAdapter(sql, connString);
break;
// case 3: // Add your custom data provider
default:
break;
}
return idbAdapter;
}
}
}

** 用户应用程序 ** ** : **

现在,就让我们来看看如何在 windows 应用程序里面使用这个类。为了进行测试,我们创建了一个 windows 应用程序,程序介面如下图 :

在窗体上我们放上三个 radio buttons 控件,一个 button 控件 , 一个 Group Box 控件和一个 DataGrid 控件。

从窗体的部局上我们可以猜的出,此程序可以根据用户的选择来确定使用哪种数据提供者。正如你在上图所看到的,窗体上有三个选项,你可以选择其一,然后单击 connect 按钮。根据选择的连接类型,联结到数据库关且向 DataGrid 中填充数据。

在我的应用程序里面,我定义了下面的变量。

private string connString, sql;
IDbConnection conn = null ;
IDbDataAdapter adapter = null ;

下面就是 connect 按钮的的代码,在哪里创建了一个 GenericAdoNetComp 类的实例,并且调用它的 GetConnection 和 GetDataAdapter 方法。一但你提供了一个 DataAdapter, 你只需简单的调用 Fill 和 Update 方法来读取和写入数据。

private void ConnectBtn_Click(object sender, System.EventArgs e)
{
GenericAdoNetComp genDP = new GenericAdoNetComp();
sql = "SELECT * FROM Employees";

if(radioButton1.Checked)
{
connString =
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\Northwind.mdb";
conn = genDP.GetConnection(1, connString);
adapter = genDP.GetDataAdapter(1, connString, sql);
}
else if (radioButton2.Checked)
{
connString =
"Data Source=MCB;Initial Catalog=Northwind;user id=sa;password=;";
conn = genDP.GetConnection(2, connString);
adapter = genDP.GetDataAdapter(2, connString, sql);
}
else if (radioButton3.Checked)
{
// Construct your connection string here
conn = genDP.GetConnection(3, connString);
adapter = genDP.GetDataAdapter(3, connString, sql);
}

try
{
conn.Open();
// Fill a DataSet
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
finally
{
conn.Close();
}
}

** 总结 ** :

在这篇文章里,我们讨论了如何写一个通用数据访问类。你可以扩展用 ADO.net 中其实的组组件来这个类的功能,我一直努力使这篇文章通俗易懂。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus