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

关键字: 通用数据库类

接上一篇文章

OleDbDataProvider.cs 提供OLEDB连接访问的类

c# 代码
  1. #define DEBUG   
  2. using System;   
  3. using System.Data;   
  4. using System.Data.OleDb;   
  5.   
  6. namespace DataProviders   
  7. {   
  8.  ///    
  9.  /// OleDbDataProvider 的摘要说明。   
  10.  ///    
  11.  internal class OleDbDataProvider : IDataProvider   
  12.  {   
  13.      
  14.   private System.Data.OleDb.OleDbConnection oleDbConnection;   
  15.   private System.Data.OleDb.OleDbCommand oleDbCommand;   
  16.   private string connectionString;   
  17.   public OleDbDataProvider() : this(null)   
  18.   {   
  19.    //   
  20.    // TODO: 在此处添加构造函数逻辑   
  21.    //   
  22.   }     
  23.     
  24.   public OleDbDataProvider(string connectionString)   
  25.   {    
  26.    if (connectionString == null || connectionString.Trim() == string.Empty)   
  27.    {   
  28.     System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();   
  29.     this.connectionString = (string)(configurationAppSettings.GetValue("oleDbConnectionString"typeof(string)));   
  30.    }   
  31.    else  
  32.    {    
  33.     this.connectionString = connectionString;   
  34.    }       
  35.   }   
  36.      
  37.   ///    
  38.   /// OleDb 连接字符串   
  39.   ///    
  40.   public string ConnectionString   
  41.   {   
  42.    get  
  43.    {   
  44.     return this.connectionString;   
  45.    }   
  46.    set  
  47.    {   
  48.     this.connectionString = value;   
  49.    }   
  50.   }    
  51.     
  52.   ///    
  53.   /// 返回一个带有连接字符串的OleDb Connection.   
  54.   ///    
  55.   ///  OracleConnection   
  56.   private OleDbConnection GetOleDbConnection()    
  57.   {   
  58.    try    
  59.    {   
  60.     return new OleDbConnection(this.connectionString);   
  61.    }    
  62.    catch (Exception ex)   
  63.    {  
  64. #if DEBUG   
  65.     System.Diagnostics.Debug.WriteLine(ex.ToString());  
  66. #endif   
  67.     return null;   
  68.    }   
  69.   }   
  70.   
  71.   ///    
  72.   /// 对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1   
  73.   ///    
  74.   /// UPDATE、INSERT 和 DELETE 语句   
  75.   public int ExecuteNonQuery(string sql)   
  76.   {   
  77.    using(oleDbConnection = this.GetOleDbConnection())   
  78.    {   
  79.     if (oleDbConnection == null)   
  80.      return -1;   
  81.     try  
  82.     {   
  83.      if (oleDbConnection.State == System.Data.ConnectionState.Closed)   
  84.       oleDbConnection.Open();        
  85.      oleDbCommand = new OleDbCommand(sql, oleDbConnection);   
  86.      return oleDbCommand.ExecuteNonQuery();   
  87.     }   
  88.     catch (Exception ex)   
  89.     {       
  90. #if DEBUG   
  91.      System.Diagnostics.Debug.WriteLine(ex.ToString());  
  92. #endif   
  93.      return -1;   
  94.     }       
  95.    }   
  96.   }   
  97.   
  98.   ///    
  99.   /// 执行查询,并将查询返回的结果集中第一行的第一列作为 .NET Framework 数据类型返回。忽略额外的列或行。   
  100.   ///    
  101.   /// SELECT 语句   
  102.   ///  .NET Framework 数据类型形式的结果集第一行的第一列;如果结果集为空或结果为 REF CURSOR,则为空引用   
  103.   public object ExecuteScalar(string sql)   
  104.   {      
  105.    using(oleDbConnection = this.GetOleDbConnection())   
  106.    {   
  107.     if (oleDbConnection == null)   
  108.      return null;   
  109.     try  
  110.     {   
  111.      if (oleDbConnection.State == System.Data.ConnectionState.Closed)   
  112.       oleDbConnection.Open();        
  113.      oleDbCommand = new OleDbCommand(sql, oleDbConnection);   
  114.      return oleDbCommand.ExecuteScalar();   
  115.     }   
  116.     catch (Exception ex)   
  117.     {       
  118. #if DEBUG   
  119.      System.Diagnostics.Debug.WriteLine(ex.ToString());  
  120. #endif   
  121.      return null;   
  122.     }       
  123.    }   
  124.   }   
  125.   
  126.   ///    
  127.   ///  执行单Sql语句查询,并将查询返回的结果作为一个数据集返回   
  128.   ///    
  129.   /// SELECT 语句   
  130.   ///  数据集 DataSet   
  131.   public DataSet RetriveDataSet(string sql)   
  132.   {      
  133.    if (sql == null || sql == string.Empty)   
  134.    {  
  135. #if DEBUG   
  136.     System.Diagnostics.Debug.WriteLine("sql 为空");      
  137. #endif   
  138.     return null;   
  139.    }      
  140.    using(oleDbConnection = this.GetOleDbConnection())   
  141.    {     
  142.     if (oleDbConnection == null)   
  143.      return null;   
  144.     using(OleDbDataAdapter da = new OleDbDataAdapter(sql, oleDbConnection))   
  145.     {   
  146.      DataSet ds = new DataSet();   
  147.      try  
  148.      {   
  149.       da.Fill(ds);   
  150.      }        
  151.      catch (Exception ex)   
  152.      {  
  153. #if DEBUG   
  154.       System.Diagnostics.Debug.WriteLine(ex.ToString());  
  155. #endif   
  156.      }        
  157.      return ds;   
  158.     }      
  159.    }      
  160.   }   
  161.   
  162.   
  163.   ///    
  164.   /// 执行Sql数组语句查询,并将查询返回的结果作为一个数据集返回   
  165.   ///    
  166.   /// Select 语句数组   
  167.   /// TableName   
  168.   ///  数据集 DataSet   
  169.   public DataSet RetriveDataSet(string[] sql, params string[] tableName)   
  170.   {   
  171.    int sqlLength;   
  172.    sqlLength = sql.Length;   
  173.    if ( sqlLength == 0)   
  174.    {  
  175. #if DEBUG   
  176.     System.Diagnostics.Debug.WriteLine("sql 为空");  
  177. #endif   
  178.     return null;   
  179.    }     
  180.    using(oleDbConnection = this.GetOleDbConnection())   
  181.    {     
  182.     if (oleDbConnection == null)   
  183.      return null;   
  184.     DataSet ds = new DataSet();   
  185.     int tableNameLength = tableName.Length;   
  186.     for (int i = 0; i < sqlLength; i++)   
  187.     {   
  188.      using(OleDbDataAdapter da = new OleDbDataAdapter(sql[i], oleDbConnection))   
  189.      {    
  190.       try  
  191.       {    
  192.        if (i < tableNameLength)   
  193.         da.Fill(ds, tableName[i]);   
  194.        else  
  195.         da.Fill(ds, "table" + i);          
  196.       }        
  197.       catch (Exception ex)   
  198.       {  
  199. #if DEBUG   
  200.        System.Diagnostics.Debug.WriteLine(ex.ToString());  
  201. #endif   
  202.        return null;   
  203.       }   
  204.      }    
  205.     }   
  206.     return ds;     
  207.    }      
  208.   }   
  209.   
  210.   public DataSet UpdateDataSet(string sql, DataSet hasChangesDataSet)   
  211.   {   
  212.    return null;   
  213.   }   
  214.   
  215.   public void Dispose()   
  216.   {   
  217.    this.connectionString = null;   
  218.    this.oleDbCommand.Dispose();   
  219.    this.oleDbConnection.Dispose();   
  220.   }   
  221.  }   
  222. }   

 

App.config 应用程序配置文件

xml 代码
  1. xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.  <appSettings>  
  4.      
  5.      
  6.      
  7.   <add key="oracleConnectionString" value="Data Source=myoracledb;User ID=me;Password=mypwd;Persist Security Info=true;" />  
  8.   <add key="sqlConnectionString" value="uid=sa;pwd=;initial catalog=mymsdb;data source=127.0.0.1;Connect Timeout=900" />  
  9.   <add key="oleDbConnectionString" value="" />  
  10.   <add key="odbcConnectionString" value="" />  
  11.  appSettings>  
  12. configuration>  

有了以上这几个类,对访问各种数据库会变得比较方便和易于管理代码,配置文件也可以随时更改,但是缺点在于配置文件使用System.Configuration只能读取,而无法在程序中改写,而把它当作xml来处理又稍显复杂,不过好在还可以用系统API调用,将属性用ini文件存储,效率高而且使用也比较简单。

(全文完)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值