分享
 
 
 

我的OLEDB SqlHelper

王朝mssql·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

我的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原著文章,假如有转载请告知,谢谢。

大家有好的文章可以发邮件给我,互相学习,共同进步!

-----------------------------------------------------------------------

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有