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

类别:.NET开发 点击:0 评论:0 推荐:

用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>
  /// 根据连接字符串建立Connection对象
  /// </summary>
  /// <param name="strConn">连接字符串</param>
  /// <returns>Connection对象</returns>
  public IDbConnection CreateConnection(string strConn)
  {
   return new OleDbConnection(strConn);
  }

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

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

  /// <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 strProvider = ConfigurationSettings.AppSettings["OleDbProvider"];
   string strDataSource = ConfigurationSettings.AppSettings["OleDbDataSource"];
   string strConnectionString = "Provider = " + strProvider + ";Data Source = " + strDataSource + ";";
   return strConnectionString;
  }

 }
}

(5)OdbcFactory.cs

using System;
using System.Data;
using System.Data.Odbc;
using System.Configuration;

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

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

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

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

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

  /// <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 strDriver = ConfigurationSettings.AppSettings["OdbcDriver"];
   string strDBQ = ConfigurationSettings.AppSettings["OdbcDBQ"];
   string strConnectionString = "Driver={" + strDriver + "}; DBQ=" + strDBQ + ";";
   return strConnectionString;   
  }

 }
}

待续......

本文地址:http://com.8s8s.com/it/it44973.htm