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

用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;   
  }

 }
}

待续......

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

chrch

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值