一个可以兼容各种数据库事务的使用范例
传入实体执行(可添加 修改 删除)事务。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
IDbHelper dbHelper = new
OracleHelper(ConfigHelper.GetConfigString( "BusinessDbConnection" )); bool
result = true ; try { dbHelper.BeginTransaction(); //主表 TE_AREAManager manager = new
TE_AREAManager(dbHelper, userInfo); TE_AREAEntity tE_AREAEntity = manager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); manager.Delete(tE_AREAEntity); //子表 TE_AREA_SUBManager submanager = new
TE_AREA_SUBManager(dbHelper, userInfo); TE_AREA_SUBEntity tE_AREA_SUBEntity = submanager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); submanager.Delete(tE_AREA_SUBEntity); //事务提交 dbHelper.CommitTransaction(); } catch (Exception ex) { //事务回滚 dbHelper.RollbackTransaction(); result= false ; } |
可以传入sql语句执行事务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
IDbHelper dbHelper = new
OracleHelper(ConfigHelper.GetConfigString( "BusinessDbConnection" )); bool
result = true ; try { dbHelper.BeginTransaction(); string
commandText = " DELETE FROM TE_AREA WHERE ID="
+ dbHelper.SqlSafe(id); dbHelper.ExecuteNonQuery(sqlString); commandText = " DELETE FROM TE_AREA_SUB WHERE ID="
+ dbHelper.SqlSafe(id); dbHelper.ExecuteNonQuery(commandText); dbHelper.CommitTransaction(); } catch (Exception ex) { dbHelper.RollbackTransaction(); result= false ; } |
还可以同时传入实体,SQL语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
IDbHelper dbHelper = new
OracleHelper(ConfigHelper.GetConfigString( "BusinessDbConnection" )); bool
result = true ; try { dbHelper.BeginTransaction(); //主表 TE_AREAManager manager = new
TE_AREAManager(dbHelper, userInfo); TE_AREAEntity tE_AREAEntity = manager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); manager.Delete(tE_AREAEntity); //子表 TE_AREA_SUBManager submanager = new
TE_AREA_SUBManager(dbHelper, userInfo); TE_AREA_SUBEntity tE_AREA_SUBEntity = submanager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); submanager.Delete(tE_AREA_SUBEntity); //执行SQL语句 string
commandText = " DELETE FROM JINTIANDA WHERE ID="
+ dbHelper.SqlSafe(id); dbHelper.ExecuteNonQuery(commandText); //事务提交 dbHelper.CommitTransaction(); } catch (Exception ex) { //事务回滚 dbHelper.RollbackTransaction(); result= false ; } |
以上事务处理方法是基于吉日嘎拉通用权限管理组件底层的代码上实现的,dbHelper,兼容各种数据库事务处理,非常省心省事。
欢迎大家提供自己的使用经验,共同提高开发效率。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。