java dbhelper通用类,DbHelper数据库通用类使用方法

本文档展示了如何使用DbHelper类执行SQL语句、存储过程,获取DataTable、DataSet,处理DataReader以及利用事务进行数据操作。示例代码涵盖了参数化查询、返回值处理和事务的提交与回滚。
摘要由CSDN通过智能技术生成

代码

//

执行SQL语句

public

static

void

ExecSqlCommand()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetSqlStringCommond(

"

select * from t1_insert

"

);

db.ExecuteNonQuery(command);

}

//

执行存储过程

public

static

void

ExecStoredProcedure()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetStoredProcCommond(

"

t1_insert

"

);

db.AddInParameter(command,

"

@id

"

, DbType.String,

"

heihei

"

);

db.AddInParameter(command,

"

@id

"

, DbType.String,

"

heihei

"

);

db.ExecuteNonQuery(command);

}

//

返回DataTable

public

static

void

GetDataTable()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetSqlStringCommond(

"

t1_findall

"

);

DataTable dt

=

db.ExecuteDataTable(command);

}

//

获取各种返回值

public

static

void

GetAllParameter()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetStoredProcCommond(

"

t2_insert

"

);

db.AddInParameter(command,

"

@timeticks

"

, DbType.Int64, DateTime.Now.Ticks);

db.AddOutParameter(command,

"

@outString

"

, DbType.String,

20

);

db.AddReturnParameter(command,

"

@returnValue

"

, DbType.Int32);

db.ExecuteNonQuery(command);

string

s = db.GetParameter(command,

"

@outString

"

).Value

as

string

;

//

out parameter

int

r = Convert.ToInt32(db.GetParameter(command,

"

@returnValue

"

).Value);

//

return value

}

//

DateReader操作

public

static

void

GetDataReader()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetStoredProcCommond(

"

t2_insert

"

);

db.AddInParameter(command,

"

@timeticks

"

, DbType.Int64, DateTime.Now.Ticks);

db.AddOutParameter(command,

"

@outString

"

, DbType.String,

20

);

db.AddReturnParameter(command,

"

@returnValue

"

, DbType.Int32);

using

(DbDataReader reader =

db.ExecuteReader(command))

{

//

dt.Load(reader);

}

string

s = db.GetParameter(command,

"

@outString

"

).Value

as

string

;

//

out parameter

int

r = Convert.ToInt32(db.GetParameter(command,

"

@returnValue

"

).Value);

//

return value

}

//

获取DataSet

public

static

void

GetDataSet()

{

DbHelper db

=

new

DbHelper();

DbCommand command

= db.GetSqlStringCommond(

"

select * from t1

"

);

DataSet ds

=

db.ExecuteDataSet(command);

}

//

事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)

public

void

DoBusiness()

{

using

(Trans t =

new

Trans())

{

try

{

D1(t);

throw

new

Exception();

//

如果有异常,会回滚滴

D2(t);

t.Commit();

}

catch

{

t.RollBack();

}

}

}

public

void

D1(Trans t)

{

DbHelper db

=

new

DbHelper();

DbCommand cmd

= db.GetStoredProcCommond(

"

t2_insert

"

);

db.AddInParameter(cmd,

"

@timeticks

"

, DbType.Int64, DateTime.Now.Ticks);

db.AddOutParameter(cmd,

"

@outString

"

, DbType.String,

20

);

db.AddReturnParameter(cmd,

"

@returnValue

"

, DbType.Int32);

if

(t ==

null

)

db.ExecuteNonQuery(cmd);

else

db.ExecuteNonQuery(cmd,t);

string

s = db.GetParameter(cmd,

"

@outString

"

).Value

as

string

;

//

out parameter

int

r = Convert.ToInt32(db.GetParameter(cmd,

"

@returnValue

"

).Value);

//

return value

}

public

void

D2(Trans t)

{

DbHelper db

=

new

DbHelper();

DbCommand cmd

= db.GetSqlStringCommond(

"

insert t1 (id)values(‘..‘)

"

);

if

(t ==

null

)

db.ExecuteNonQuery(cmd);

else

db.ExecuteNonQuery(cmd, t);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值