日期:2009-09-11  浏览次数:20427 次

用C#实现的数据库抽象工厂(二)

以下3个类分别是Factory针对SqlServer专用连接、OleDb连接和Odbc连接时的具体实现:


(3)SqlFactory.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DbService
{
/// <summary>
/// 针对SqlServer专用连接的工厂
/// </summary>
public class SqlFactory : AbstractDbFactory
{
/// <summary>
/// 构造函数
/// </summary>
public SqlFactory()
{
}

/// <summary>
/// 建立默认Connection对象
/// </summary>
/// <returns>Connection对象</returns>
public IDbConnection CreateConnection()
{
return new SqlConnection();
}

/// <summary>
/// 根据连接字符串建立Connection对象
/// </summary>
/// <param name="strConn">连接字符串</param>
/// <returns>Connection对象</returns>
public IDbConnection CreateConnection(string strConn)
{
return new SqlConnection(strConn);
}

/// <summary>
/// 建立Command对象
/// </summary>
/// <returns>Command对象</returns>
public IDbCommand CreateCommand()
{
return new SqlCommand();
}

/// <summary>
/// 建立DataAdapter对象
/// </summary>
/// <returns>DataAdapter对象</returns>
public IDbDataAdapter CreateDataAdapter()
{
return new SqlDataAdapter();
}

/// <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();
}

/// <summary>
/// 获得连接字符串
/// </summary>
/// <returns>连接字符串</returns>
public string GetConnectionString()
{
string strServer = ConfigurationSettings.AppSettings["SqlServerServer"];
string strDatabase = ConfigurationSettings.AppSettings["SqlServerDatabase"];
string strUid = ConfigurationSettings.AppSettings["SqlServerUid"];
string strPwd = ConfigurationSettings.AppSettings["SqlServerPwd"];
string strConnectionString = "Server = " + strServer + "; Database = " + strDatabase + "; Uid = " + strUid + "; Pwd = " + strPwd + ";";
return strConnectionString;
}

}
}


(4)OleDbFactory.cs

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace DbService
{
/// <summary>
/// 针对OleDb连接的工厂
/// </summary>
public class OleDbFactory : AbstractDbFactory
{
/// <summary>
/// 构造函数
/// </summary>
public OleDbFactory()
{
}

/// <summary>
/// 建立默认Connection对象
/// </summary>
/// <returns>Connection对象</returns>
public IDbConnection CreateConnection()
{
return new OleDbConnection();
}

/// <summary>
/// 根据连接字符串建