.NET通用访问数据库类(一)

关键字: 通用数据库类

在数据库应用程序的开发中,往往一个应用程序要从多个数据库中取得所要的业务数据。对于ADO.NET更是如此,一个DataSet,可能是从几个数据库中取得的数据集合。为此开发一个访问数据库通用接口显得更为方便、灵活。下面是一个针对.NET提供的四种访问数据库的通用类,每个类都有具体实现了一些常用访问数据库的方法。

DataProvider.cs 此类返回一个访问数据库的接口实例。

c# 代码
  1. #define DEBUG   
  2. using System;   
  3. namespace DataProviders   
  4. {   
  5.  ///    
  6.  /// 提供对数据库访问的通用类。   
  7.  ///    
  8.  public class DataProvider   
  9.  {    
  10.   ///    
  11.   /// 数据库枚举类型   
  12.   ///    
  13.   public enum DataProviderType   
  14.   {   
  15.    OdbcDataProvider = 0,   
  16.    OleDbDataProvider = 1,   
  17.    OracleDataProvider = 2,   
  18.    SqlDataProvider = 3     
  19.   }   
  20.   
  21.   ///    
  22.   /// 建立访问数据库的实例   
  23.   ///    
  24.   /// 数据库枚举类型   
  25.   ///    
  26.   public static IDataProvider CreateDataProvider(DataProviderType dataProviderType)   
  27.   {   
  28.    switch (dataProviderType)   
  29.    {   
  30.     case DataProviderType.OdbcDataProvider:   
  31.      return new OdbcDataProvider();        
  32.     case DataProviderType.OleDbDataProvider:   
  33.      return new OleDbDataProvider();      
  34.     case DataProviderType.OracleDataProvider:   
  35.      return new OracleDataProvider();        
  36.     case DataProviderType.SqlDataProvider:   
  37.      return new SqlDataProvider();        
  38.     default:  
  39. #if DEBUG   
  40.      System.Diagnostics.Debug.WriteLine("dataProviderType 类型不存在!");  
  41. #endif    
  42.      return null;   
  43.    }   
  44.   }   
  45.   
  46.  }   
  47. }   
  48.   

 

IDataProvider.cs 访问数据库的接口类

c# 代码
  1. using System;   
  2.   
  3. namespace DataProviders   
  4. {   
  5.  ///    
  6.  /// 对数据库访问的通用接口   
  7.  ///    
  8.  public interface IDataProvider   
  9.  {    
  10.   ///    
  11.   /// 执行 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数   
  12.   ///      
  13.   int ExecuteNonQuery(string sql);   
  14.   
  15.   ///    
  16.   /// 执行查询,并将查询返回的结果集中第一行的第一列作为 .NET Framework 数据类型返回。忽略额外的列或行   
  17.   ///      
  18.   object ExecuteScalar(string sql);   
  19.      
  20.   ///    
  21.   ///  执行单Sql语句查询,并将查询返回的结果作为一个数据集返回   
  22.   ///      
  23.   System.Data.DataSet RetriveDataSet(string sql);    
  24.      
  25.   ///    
  26.   ///  执行Sql数组语句查询,并将查询返回的结果作为一个数据集返回     
  27.   ///      
  28.   System.Data.DataSet RetriveDataSet(string[] sql, params string[] tableName);   
  29.   
  30.   ///    
  31.   /// 更新库   
  32.   ///    
  33.   ///    
  34.   ///    
  35.   ///    
  36.   System.Data.DataSet UpdateDataSet(string sql, System.Data.DataSet hasChangesDataSet);   
  37.   
  38.   ///    
  39.   /// 执行Dispose   
  40.   ///    
  41.   void Dispose();     
  42.  }   
  43. }   
  44.   

 

OracleDataProvider.cs 访问Oracle的类

c# 代码
  1. #define DEBUG   
  2. using System;   
  3. using System.Data;   
  4. using System.Data.OracleClient;   
  5.   
  6. namespace DataProviders   
  7. {   
  8.  ///    
  9.  /// OracleDataProvider 的摘要说明。   
  10.  ///    
  11.  internal class OracleDataProvider : IDataProvider   
  12.  {   
  13.   private System.Data.OracleClient.OracleConnection oracleConnection;   
  14.   private System.Data.OracleClient.OracleCommand oracleCommand;   
  15.   private string connectionString;   
  16.   public OracleDataProvider() : this(null)   
  17.   {   
  18.    //   
  19.    // TODO: 在此处添加构造函数逻辑   
  20.    //   
  21.   }   
  22.   
  23.   public OracleDataProvider(string connectionString)   
  24.   {    
  25.    if (connectionString == null || connectionString.Trim() == string.Empty)   
  26.    {   
  27.     System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();   
  28.     this.connectionString = (string)(configurationAppSettings.GetValue("oracleConnectionString"typeof(string)));   
  29.    }   
  30.    else  
  31.    {    
  32.     this.connectionString = connectionString;   
  33.    }       
  34.   }   
  35.      
  36.   ///    
  37.   /// Oracle 连接字符串 "User Id=southfence;Data Source=FENCEORA;Password=southfence;Persist Security Info=true;"       
  38.   ///    
  39.   public string ConnectionString   
  40.   {   
  41.    get{   
  42.     return this.connectionString;   
  43.    }   
  44.    set{   
  45.     this.connectionString = value;   
  46.    }   
  47.   }    
  48.     
  49.   ///    
  50.   /// 返回一个带有连接字符串的Oracle Connection.   
  51.   ///    
  52.   ///  OracleConnection   
  53.   private OracleConnection GetOracleConnection()    
  54.   {   
  55.    try    
  56.    {   
  57.     return new OracleConnection(this.connectionString);   
  58.    }    
  59.    catch (Exception ex)   
  60.    {  
  61. #if DEBUG   
  62.     System.Diagnostics.Debug.WriteLine(ex.ToString());  
  63. #endif   
  64.     return null;   
  65.    }   
  66.   }   
  67.   
  68.   ///    
  69.   /// 对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1   
  70.   ///    
  71.   /// UPDATE、INSERT 和 DELETE 语句   
  72.   public int ExecuteNonQuery(string sql)   
  73.   {   
  74.    using(oracleConnection = this.GetOracleConnection())   
  75.    {   
  76.     if (oracleConnection == null)   
  77.      return -1;   
  78.     int rv = -1;   
  79.     OracleTransaction oracleTransaction = null;      
  80.     try  
  81.     {   
  82.      if (oracleConnection.State == System.Data.ConnectionState.Closed)   
  83.       oracleConnection.Open();        
  84.      oracleCommand = new OracleCommand(sql, oracleConnection);       
  85.      oracleTransaction = oracleConnection.BeginTransaction();   
  86.      oracleCommand.Transaction = oracleTransaction;   
  87.      rv = oracleCommand.ExecuteNonQuery();   
  88.      oracleTransaction.Commit();        
  89.     }   
  90.     catch (Exception ex)   
  91.     {       
  92. #if DEBUG   
  93.      System.Diagnostics.Debug.WriteLine(ex.ToString());  
  94. #endif   
  95.      oracleTransaction.Rollback();   
  96.      rv = -1;   
  97.     }       
  98.   
  99.     return rv;   
  100.    }   
  101.   }   
  102.   
  103.   ///    
  104.   /// 执行查询,并将查询返回的结果集中第一行的第一列作为 .NET Framework 数据类型返回。忽略额外的列或行。   
  105.   ///    
  106.   /// SELECT 语句   
  107.   ///  .NET Framework 数据类型形式的结果集第一行的第一列;如果结果集为空或结果为 REF CURSOR,则为空引用   
  108.   public object ExecuteScalar(string sql)   
  109.   {      
  110.    using(oracleConnection = this.GetOracleConnection())   
  111.    {   
  112.     if (oracleConnection == null)   
  113.      return null;   
  114.     try  
  115.     {   
  116.      if (oracleConnection.State == System.Data.ConnectionState.Closed)   
  117.       oracleConnection.Open();        
  118.      oracleCommand = new OracleCommand(sql, oracleConnection);   
  119.      return oracleCommand.ExecuteScalar();   
  120.     }   
  121.     catch (Exception ex)   
  122.     {       
  123. #if DEBUG   
  124.      System.Diagnostics.Debug.WriteLine(ex.ToString());  
  125. #endif   
  126.      return null;   
  127.     }       
  128.    }   
  129.   }   
  130.   
  131.   ///    
  132.   ///  执行单Sql语句查询,并将查询返回的结果作为一个数据集返回   
  133.   ///    
  134.   /// SELECT 语句   
  135.   ///  数据集 DataSet   
  136.   public DataSet RetriveDataSet(string sql)   
  137.   {      
  138.    if (sql == null || sql == string.Empty)   
  139.    {  
  140. #if DEBUG   
  141.     System.Diagnostics.Debug.WriteLine("sql 为空");      
  142. #endif   
  143.     return null;   
  144.    }      
  145.    using(oracleConnection = this.GetOracleConnection())   
  146.    {     
  147.     if (oracleConnection == null)   
  148.      return null;   
  149.     using( OracleDataAdapter da = new OracleDataAdapter(sql, oracleConnection))   
  150.     {   
  151.      DataSet ds = new DataSet();   
  152.      try  
  153.      {   
  154.       da.Fill(ds);   
  155.      }        
  156.      catch (Exception ex)   
  157.      {  
  158. #if DEBUG   
  159.       System.Diagnostics.Debug.WriteLine(ex.ToString());  
  160. #endif   
  161.      }        
  162.      return ds;   
  163.     }      
  164.    }      
  165.   }    
  166.     
  167.   ///    
  168.   /// 执行Sql数组语句查询,并将查询返回的结果作为一个数据集返回   
  169.   ///    
  170.   /// Select 语句数组   
  171.   /// TableName   
  172.   ///  数据集 DataSet   
  173.   public DataSet RetriveDataSet(string[] sql, params string[] tableName)   
  174.   {   
  175.    int sqlLength;      
  176.    sqlLength = sql.Length;   
  177.    if ( sqlLength == 0)   
  178.    {  
  179. #if DEBUG   
  180.     System.Diagnostics.Debug.WriteLine("sql 为空");  
  181. #endif   
  182.     return null;   
  183.    }    
  184.    using(oracleConnection = this.GetOracleConnection())   
  185.    {     
  186.     if (oracleConnection == null)   
  187.      return null;   
  188.     DataSet ds = new DataSet();   
  189.     int tableNameLength = tableName.Length;   
  190.     for (int i = 0; i < sqlLength; i++)   
  191.     {   
  192.      using(OracleDataAdapter da = new OracleDataAdapter(sql[i], oracleConnection))   
  193.      {    
  194.       try  
  195.       {          
  196.        if (i < tableNameLength)   
  197.         da.Fill(ds, tableName[i]);   
  198.        else  
  199.         da.Fill(ds, "table" + i);          
  200.       }        
  201.       catch (Exception ex)   
  202.       {  
  203. #if DEBUG   
  204.        System.Diagnostics.Debug.WriteLine(ex.ToString());  
  205. #endif   
  206.        return null;   
  207.       }   
  208.      }    
  209.     }   
  210.     return ds;     
  211.    }      
  212.   }   
  213.   
  214.   ///    
  215.   /// 更新数据集.    
  216.   /// 过程:客户层(dataSet.GetChanges()) -- 修改 --> 数据服务层(hasChangesDataSet.update()) -- 更新--> 数据层(hasChangesDataSet) ...   
  217.   ///  数据层(hasChangesDataSet) -- 新数据 --> 数据服务层 (hasChangesDataSet) -- 合并 -- > 客户层(dataSet.Merge(hasChangesDataSet))   
  218.   ///    
  219.   ///    
  220.   ///    
  221.   public DataSet UpdateDataSet(string sql, DataSet hasChangesDataSet)   
  222.   {   
  223.          
  224.   }   
  225.   
  226.   ///    
  227.   ///  执行Sql数组语句查询,并将查询返回的结果作为一个数据读取器返回   
  228.   ///    
  229.   ///    
  230.   ///  OracleDataReader   
  231.   public OracleDataReader RetriveDataReader(string sql)   
  232.   {   
  233.    if (sql == null || sql == string.Empty)   
  234.    {  
  235. #if DEBUG   
  236.     System.Diagnostics.Debug.WriteLine("sql 为空");      
  237. #endif   
  238.     return null;   
  239.    }      
  240.    using(oracleConnection = this.GetOracleConnection())   
  241.    {     
  242.     if (oracleConnection == null)   
  243.      return null;   
  244.     using(oracleCommand = new OracleCommand(sql, oracleConnection))   
  245.     {      
  246.      try  
  247.      {   
  248.       OracleDataReader oracleDataReader = oracleCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);   
  249.       return oracleDataReader;   
  250.      }        
  251.      catch (Exception ex)   
  252.      {  
  253. #if DEBUG   
  254.       System.Diagnostics.Debug.WriteLine(ex.ToString());  
  255. #endif   
  256.       return null;   
  257.   
  258.      }   
  259.     }    
  260.    }      
  261.   }   
  262.   
  263.   public void Dispose()   
  264.   {   
  265.    this.connectionString = null;   
  266.    this.oracleCommand.Dispose();   
  267.    this.oracleConnection.Dispose();   
  268.   }   
  269.  }   
  270. }     

 

(未完待续。。。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值