文章索引 聚合服务页

工 具 文 档

您现在的位置: 创作设计空间 >> 站长教程 >> 编程技术 >> .NET >> 正文
站内文章搜索:

我的OLEDB SqlHelper

作者:佚名    文章来源:站长之家    点击数:11198    更新时间:2009/1/6

我的OLEDB SqlHelper

2005-07-21  njbaige/白鸽  于苏州HJ

最近看了MS的Enterprise Library ,里面有一套很强大的Data Access Application Block
提供了很强大的基于SQL,DataObject(DataSet,DataReader)接口的快速访问数据库的
方法,并且同Configuration Application Block相结合,非常之灵活。但是灵活的同时也
增加了学习的难度和配置的复杂度。
在平常编写系统过程中,我也积累了一套SqlHelper,暂时是基于OLEDB(Connetcion).
因为它比较灵活,SQL 2000也能使用。
我的SqlHelper是轻量级的,仿Data Access Application Block的代码库。提供非常友好
的接口(不是基于ORM级别的).
下面附上相关代码,供大家学习,参考。欢迎提出修改意见:

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


 namespace MyLib.Data
 {

  public sealed class CDBConn
  {
   // Methods
   public CDBConn()
   {
    this.m_bIsInTransaction = false;
    this.m_Timeout = 30;
    this.m_objConn = new OleDbConnection(ConfigurationSettings.AppSettings["ConnectString"]);
    this.m_objConn.Open();
   }

   public CDBConn(bool bOpenNow)
   {
    this.m_bIsInTransaction = false;
    this.m_Timeout = 30;
    this.m_objConn = new OleDbConnection(ConfigurationSettings.AppSettings["ConnectString"]);
    if (bOpenNow)
    {
     this.m_objConn.Open();
    }
   }

   public CDBConn(string strConnectionString)
   {
    this.m_bIsInTransaction = false;
    this.m_Timeout = 30;
    this.m_objConn = new OleDbConnection(strConnectionString);
    this.m_objConn.Open();
   }

   public CDBConn(string strConnectionString, bool bOpenNow)
   {
    this.m_bIsInTransaction = false;
    this.m_Timeout = 30;
    this.m_objConn = new OleDbConnection(strConnectionString);
    if (bOpenNow)
    {
     this.m_objConn.Open();
    }
   }

   public void BeginTransaction()
   {
    if (!this.m_bIsInTransaction)
    {
     this.m_objCommand = new OleDbCommand();
     this.m_objCommand.Connection = this.m_objConn;
     this.m_objTransaction = this.m_objConn.BeginTransaction();
     if (this.m_Timeout >= 30)
     {
      this.m_objCommand.CommandTimeout = this.m_Timeout;
     }
     this.m_objCommand.Transaction = this.m_objTransaction;
     this.m_bIsInTransaction = true;
    }
   }

       

   public void Close()
   {
    if (this.m_objConn.State != ConnectionState.Closed)
    {
     this.m_objConn.Close();
    }
   }

   public void Commit()
   {
    if (this.m_bIsInTransaction)
    {
     this.m_objTransaction.Commit();
     this.m_bIsInTransaction = false;
     this.m_objCommand.Dispose();
    }
   }

    

   public void Dispose()
   {
    if (this.m_objConn.State != ConnectionState.Closed)
    {
     this.m_objConn.Close();
    }
    this.m_objConn.Dispose();
   }

   public DataSet ExecuteDataSet(string strSQL)
   {
           
    DataSet set1 = new DataSet();
    OleDbCommand command1 = new OleDbCommand(strSQL, this.m_objConn);
    if (this.m_Timeout >= 30)
    {
     command1.CommandTimeout = this.m_Timeout;
    }
    OleDbDataAdapter adapter1 = new OleDbDataAdapter(command1);
    adapter1.Fill(set1);
    command1.Dispose();
    adapter1.Dispose();
    return set1;
   }

   public DataSet ExecuteDataSet(string strSQL, string strTable)
   {
    DataSet set1 = new DataSet();
    OleDbCommand command1 = new OleDbCommand(strSQL, this.m_objConn);
    if (this.m_Timeout >= 30)
    {
     command1.CommandTimeout = this.m_Timeout;
    }
    OleDbDataAdapter adapter1 = new OleDbDataAdapter(command1);
    adapter1.Fill(set1, strTable);
    command1.Dispose();
    adapter1.Dispose();
    return set1;
   }

   public OleDbDataReader ExecuteReader(string strSQL)
   {
    OleDbCommand command1 = new OleDbCommand(strSQL, this.m_objConn);
    if (this.m_Timeout >= 30)
    {
     command1.CommandTimeout = this.m_Timeout;
    }
    OleDbDataReader reader1 = command1.ExecuteReader();
    command1.Dispose();
    return reader1;
   }

   public void ExecuteSQL(string strSQL)
   {
    if (!this.m_bIsInTransaction)
    {
     OleDbCommand command1 = new OleDbCommand(strSQL, this.m_objConn);
     if (this.m_Timeout >= 30)
     {
      command1.CommandTimeout = this.m_Timeout;
     }
     command1.ExecuteNonQuery();
     command1.Dispose();
    }
    else
    {
     this.m_objCommand.CommandText = strSQL;
     this.m_objCommand.ExecuteNonQuery();
    }
   }

   public int FillDataSet(ref DataSet objDataSet, string strSQL)
   {
 
    OleDbCommand command1 = new OleDbCommand(strSQL, this.m_objConn);
    if (this.m_Timeout >= 30)
    {
     command1.CommandTimeout = this.m_Timeout;
    }
    OleDbDataAdapter adapter1 = new OleDbDataAdapter(command1);
    int num1 = adapter1.Fill(objDataSet);
    command1.Dispose();
    adapter1.Dispose();
    return num1;
   }

   public int FillDataSet(ref DataSet objDataSet, string strSQL, string strTable)
   {
 
    OleDbCommand command1 = new OleDbCommand(strSQL, this.m_objConn);
    if (this.m_Timeout >= 30)
    {
     command1.CommandTimeout = this.m_Timeout;
    }
    OleDbDataAdapter adapter1 = new OleDbDataAdapter(command1);
    int num1 = adapter1.Fill(objDataSet, strTable);
    command1.Dispose();
    adapter1.Dispose();
    return num1;
   }

   ~CDBConn()
   {
   }

   public bool Lock(string[] strArrTableName)
   {
    return true;
    //  return this.m_objSync.Lock(strArrTableName);
   }
 
   public void Open()
   {
    if (this.m_objConn.State != ConnectionState.Open)
    {
     this.m_objConn.Open();
    }
   }
 
   public void Rollback()
   {
    if (this.m_bIsInTransaction)
    {
     this.m_objTransaction.Rollback();
     this.m_bIsInTransaction = false;
     this.m_objCommand.Dispose();
    }
   }

   public void Update(string strSelectSQL, DataSet objDataSet)
   {
    OleDbDataAdapter adapter1;
    OleDbCommandBuilder builder1;
    if (!this.m_bIsInTransaction)
    {
     OleDbCommand command1 = new OleDbCommand(strSelectSQL, this.m_objConn);
     if (this.m_Timeout >= 30)
     {
      command1.CommandTimeout = this.m_Timeout;
     }
     adapter1 = new OleDbDataAdapter(command1);
     builder1 = new OleDbCommandBuilder(adapter1);
     adapter1.InsertCommand = builder1.GetI

[1] [2]  下一页

文章录入:文章录入:zhangmao    责任编辑:zm 
相关文章:
没有相关文章