我的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.GetInsertCommand();
adapter1.DeleteCommand = builder1.GetDeleteCommand();
adapter1.UpdateCommand = builder1.GetUpdateCommand();
adapter1.Update(objDataSet);
builder1.Dispose();
command1.Dispose();
adapter1.Dispose();
}
else
{
adapter1 = new OleDbDataAdapter(this.m_objCommand);
builder1 = new OleDbCommandBuilder(adapter1);
adapter1.InsertCommand = builder1.GetInsertCommand();
adapter1.DeleteCommand = builder1.GetDeleteCommand();
adapter1.UpdateCommand = builder1.GetUpdateCommand();
adapter1.Update(objDataSet);
builder1.Dispose();
adapter1.Dispose();
}
}
// Properties
public ConnectionState State
{
get
{
return this.m_objConn.State;
}
}
public int Timeout
{
get
{
return this.m_Timeout;
}
set
{
if (value < 1)
{
this.m_Timeout = 1;
}
else
{
this.m_Timeout = value;
}
}
}
// Fields
private bool m_bIsInTransaction;
private OleDbCommand m_objCommand;
private OleDbConnection m_objConn;
private OleDbTransaction m_objTransaction;
private int m_Timeout;
}
}
-----------------------------------------------------------------------
欢迎交流.net开发经验.
qq: 32660346
msn: njbaige@hotmail.com
Mial:njbaige@126.com
原著文章,如果有转载请告知,谢谢。
大家有好的文章可以发邮件给我,互相学习,共同进步!
-----------------------------------------------------------------------