采用泛型链接多类型数据库[含源码]
最近在看vs2005上关于泛型的介绍,同时考虑到手头上使用的DBSERVICE库中文件过多(共7个CS文件),想利用泛型技术对它进行“瘦身”,最后系统只有2个文件。现把相关文件做一简单说明:
说明:DbAccess.cs文件, InitailDB函数采用反射进行相关信息搜集
1 /// <summary>
2 /// 初始化数据库信息
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 /// <param name="dai"></param>
6 public static void InitailDB<T>(T dai)
7 {
8 Type type = dai.GetType();
9 object[] args = null;
10 concreteDbConn = (IDbConnection)type.GetMethod("CreateConnection").Invoke(dai, args);
11 concreteDbConn.ConnectionString = (string) type.GetMethod("GetConnectionString").Invoke(dai, args);
12 concreteDbConn.Open();
13 concreteDbCommand = (IDbCommand)type.GetMethod("CreateCommand").Invoke(dai, args);
14 concreteDbTrans = (IDbTransaction)type.GetMethod("CreateTransaction").Invoke(dai, new object[] { concreteDbConn });
15 concreteDbCommand.Connection = concreteDbConn;
16 concreteDbCommand.Transaction = concreteDbTrans;
17 concreteDbAdapter = (IDbDataAdapter)type.GetMethod("CreateDataAdapter").Invoke(dai, args);
18 }
19
2 /// 初始化数据库信息
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 /// <param name="dai"></param>
6 public static void InitailDB<T>(T dai)
7 {
8 Type type = dai.GetType();
9 object[] args = null;
10 concreteDbConn = (IDbConnection)type.GetMethod("CreateConnection").Invoke(dai, args);
11 concreteDbConn.ConnectionString = (string) type.GetMethod("GetConnectionString").Invoke(dai, args);
12 concreteDbConn.Open();
13 concreteDbCommand = (IDbCommand)type.GetMethod("CreateCommand").Invoke(dai, args);
14 concreteDbTrans = (IDbTransaction)type.GetMethod("CreateTransaction").Invoke(dai, new object[] { concreteDbConn });
15 concreteDbCommand.Connection = concreteDbConn;
16 concreteDbCommand.Transaction = concreteDbTrans;
17 concreteDbAdapter = (IDbDataAdapter)type.GetMethod("CreateDataAdapter").Invoke(dai, args);
18 }
19
调用方法:CreateGenericInstance(),只要将相应的类型做为参数加入到InitailDB<T>中即可
InitailDB<Generic_Access<SqlConnection, SqlCommand, SqlDataAdapter>>(
Generic_Access < SqlConnection, SqlCommand, SqlDataAdapter >.GetInstance());break;
泛型类DB_Generic.cs代码如下 :
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
public sealed class Generic_Access<Connection, Command, DataAdapter>
where Connection : class, new()
where Command : class, new()
where DataAdapter : class, new()
{
public static volatile Generic_Access<Connection, Command, DataAdapter> singleFactory = null;
private static object syncObj = new object();
public static Generic_Access<Connection, Command, DataAdapter> GetInstance()
{
if (singleFactory == null)
{
lock (syncObj)
{
if (singleFactory == null)
{
singleFactory = new Generic_Access<Connection, Command, DataAdapter>();
}
}
}
return singleFactory;
}
private Generic_Access()
{
}
public void Disponse()
{
singleFactory.Disponse();
}
/// <summary>
/// 建立默认Connection对象
/// </summary>
/// <returns>Connection对象</returns>
public Connection CreateConnection()
{
return new Connection();
}
/// <summary>
/// 建立Command对象
/// </summary>
/// <returns>Command对象</returns>
public Command CreateCommand()
{
return new Command();
}
/// <summary>
/// 建立DataAdapter对象
/// </summary>
/// <returns>DataAdapter对象</returns>
public DataAdapter CreateDataAdapter()
{
return new DataAdapter();
}
/// <summary>
/// 根据Connection建立Transaction
/// </summary>
/// <param name="myDbConnection">Connection对象</param>
/// <returns>Transaction对象</returns>
public IDbTransaction CreateTransaction(IDbConnection myDbConnection)
{
return myDbConnection.BeginTransaction();
}
/// <summary>
/// 根据Command建立DataReader
/// </summary>
/// <param name="myDbCommand">Command对象</param>
/// <returns>DataReader对象</returns>
public IDataReader CreateDataReader(IDbCommand myDbCommand)
{
return myDbCommand.ExecuteReader();
}
.
using System.Data;
using System.Configuration;
using System.Collections.Generic;
public sealed class Generic_Access<Connection, Command, DataAdapter>
where Connection : class, new()
where Command : class, new()
where DataAdapter : class, new()
{
public static volatile Generic_Access<Connection, Command, DataAdapter> singleFactory = null;
private static object syncObj = new object();
public static Generic_Access<Connection, Command, DataAdapter> GetInstance()
{
if (singleFactory == null)
{
lock (syncObj)
{
if (singleFactory == null)
{
singleFactory = new Generic_Access<Connection, Command, DataAdapter>();
}
}
}
return singleFactory;
}
private Generic_Access()
{
}
public void Disponse()
{
singleFactory.Disponse();
}
/// <summary>
/// 建立默认Connection对象
/// </summary>
/// <returns>Connection对象</returns>
public Connection CreateConnection()
{
return new Connection();
}
/// <summary>
/// 建立Command对象
/// </summary>
/// <returns>Command对象</returns>
public Command CreateCommand()
{
return new Command();
}
/// <summary>
/// 建立DataAdapter对象
/// </summary>
/// <returns>DataAdapter对象</returns>
public DataAdapter CreateDataAdapter()
{
return new DataAdapter();
}
/// <summary>
/// 根据Connection建立Transaction
/// </summary>
/// <param name="myDbConnection">Connection对象</param>
/// <returns>Transaction对象</returns>
public IDbTransaction CreateTransaction(IDbConnection myDbConnection)
{
return myDbConnection.BeginTransaction();
}
/// <summary>
/// 根据Command建立DataReader
/// </summary>
/// <param name="myDbCommand">Command对象</param>
/// <returns>DataReader对象</returns>
public IDataReader CreateDataReader(IDbCommand myDbCommand)
{
return myDbCommand.ExecuteReader();
}
.
最后是测试代码:using DB_Generic;
1public partial class DB_Generic_Default : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 if (!Page.IsPostBack)
6 {
7
8 Response.Write(DbAccess.SelectMaxID("users", "ID") + "<BR>");
9
10 string sql = "Select top 10 * From users Order By id asc";
11 Repeater1.DataSource = DbAccess.SelectAllSqlString(sql).Tables[0].DefaultView;
12 Repeater1.DataBind();
13
14
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 if (!Page.IsPostBack)
6 {
7
8 Response.Write(DbAccess.SelectMaxID("users", "ID") + "<BR>");
9
10 string sql = "Select top 10 * From users Order By id asc";
11 Repeater1.DataSource = DbAccess.SelectAllSqlString(sql).Tables[0].DefaultView;
12 Repeater1.DataBind();
13
14
.....
详情见 源代码
谢谢大家!
如须转载,请注明出处。