dannyr's Blog
ColdFusion Delphi Flex Java .Net —— 一个都不能少!
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 93 文章- 3 评论- 316
我设计的简单事务控制
其中数据库操作使用了Microsoft.ApplicationBlocks,请参考我的《Microsoft.ApplicationBlocks使用心得》
类结构图:
相关文件:
接口:IManager.cs IPlanManager.cs
实现:Manager.cs PlanManager.cs
使用:Test.cs
其他相关文件不予说明
Test.cs:
测试客户端,可以进行单个功能类内多个方法的事务控制,如果要进行多个功能类的事务控制就需要修改基类Manager中数据库连接的获得方式,目前是简单的获取连接串后直接创建连接,实用时候应该从一个数据库连接工厂类中获得,这样多个Manager类可以共享数据库连接,进行事务控制,并可以有效地管理数据库连接池。
using
System;
using
System.Collections;
using
ReceptionPlan.Data.Components;
using
ReceptionPlan.Data.IDAL;
using
ReceptionPlan.Data.DAL;
namespace
ReceptionPlan.Data.Services
{
/**/
///
<summary>
///
ReceptionPlan 的摘要说明。
///
</summary>
public
class
ReceptionPlanService
{
public
ReceptionPlanService()
{
}
/**/
///
<summary>
///
添加计划
///
</summary>
///
<param name="plan">
计划对象
</param>
///
<returns>
计划ID
</returns>
public
static
int
AddPlan(Plan plan)
{
IPlanManager pm
=
new
PlanManager();
pm.BeginTransaction();
try
{
int
id
=
pm.AddPlan(plan);
plan.Id
=
id;
plan.Name
=
"
XXXX
"
;
pm.ModifyPlan(plan)
pm.Commit();
return
id;
}
catch
(Exception ex)
{
pm.Rollback();
throw
ex;
}
}
}
IManager.cs
using
System;
namespace
ReceptionPlan.Data.IDAL
{
/**/
///
<summary>
///
IManager 的摘要说明。
///
</summary>
public
interface
IManager
{
void
BeginTransaction();
void
Rollback();
void
Commit();
}
}
IPlanManager.cs
using
System;
using
System.Collections;
using
ReceptionPlan.Data.Components;
namespace
ReceptionPlan.Data.IDAL
{
/**/
///
<summary>
///
IPlanManager 的摘要说明。
///
</summary>
public
interface
IPlanManager : IManager
{
int
AddPlan(Plan plan);
int
ModifyPlan(Plan plan);
}
}
Manager.cs
using
System;
using
System.Data;
using
System.Data.SqlClient;
using
System.Collections;
using
ReceptionPlan.Data.IDAL;
using
ReceptionPlan.Data.Utility;
namespace
ReceptionPlan.Data.DAL
{
public
abstract
class
Manager : IManager
{
protected
SqlTransaction trans;
protected
SqlConnection conn;
protected
ArrayList connList;
public
Manager()
{
connList
=
new
ArrayList();
}
~
Manager()
{
if
(trans
!=
null
)
trans.Rollback();
/**/
/*
foreach(SqlConnection temp in connList)
{
if(temp !=null && temp.State != ConnectionState.Closed )
temp.Close();
}
*/
}
//
public SqlConnection Connection
public
string
Connection
{
get
{
return
Configs.CONN_STR;
/**/
/*
SqlConnection temp = new SqlConnection(Configs.CONN_STR);
connList.Add(temp);
return temp;
*/
}
}
public
void
BeginTransaction()
{
conn
=
new
SqlConnection(Configs.CONN_STR);
conn.Open();
trans
=
conn.BeginTransaction();
}
public
void
Rollback()
{
if
(trans
!=
null
)
{
trans.Rollback();
trans
=
null
;
conn.Close();
}
}
public
void
Commit()
{
if
(trans
!=
null
)
{
trans.Commit();
trans
=
null
;
conn.Close();
}
}
}
}
PlanManager.cs
using
System;
using
System.Collections;
using
Microsoft.ApplicationBlocks.Data;
using
System.Data;
using
System.Data.SqlClient;
using
ReceptionPlan.Data.Components;
using
ReceptionPlan.Data.IDAL;
using
ReceptionPlan.Data.Utility;
namespace
ReceptionPlan.Data.DAL
{
/**/
///
<summary>
///
PlanManager 的摘要说明。
///
</summary>
public
class
PlanManager : Manager, IPlanManager
{
public
PlanManager()
{
}
public
int
AddPlan(Plan plan)
{
int
id
=
-
1
;
string
sql
=
string
.Empty;
try
{
sql
=
string
.Format(
"
insert into [RP_Plan] XXXX);
object
AID
=
SqlHelper.ExecuteScalar(trans, CommandType.Text, sql );
id
=
Convert.ToInt32(AID);
}
catch
(Exception ex)
{
throw
new
Exception(ex.ToString()
+
"
\n sql=
"
+
sql);
}
return
id;
}
public
int
ModifyPlan(Plan plan)
{
if
(plan.Id
<
0
)
return
-
1
;
string
sql
=
string
.Empty;
int
ret
=
-
1
;
try
{
sql
=
string
.Format(
"
update RP_Plan XXXX);
ret
=
SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql );
}
catch
(Exception ex)
{
throw
new
Exception(ex.ToString()
+
"
\n sql=
"
+
sql);
}
return
ret;
}
}
}
posted @ 2004-11-02 09:29
dannyr|一个都不能少!
阅读(1148)
评论(0)
编辑
收藏
网摘
所属分类:
.Net技术
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
人才
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2004-11-02 09:34 编辑过
Google站内搜索
China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
相关文章:
相关链接:
所属分类的其他文章:
Net1.1添加目录共享,并设置访问权限
Access数据库的文本、备注数据类型的COLUMN_FLAGS说明
如何关闭子线程?征集析构函数与多线程的讨论!
function object(functor) ...
Dev GridControl的Outlook风格定制
WinForm MDI动态加载form
关于JSON
新的Visual Assist X的确很cool
求解(暂放.net主页,谢谢支持)
关于Flex、Tomcat、Jrun、ColdFusion、IIS、ASP.Net结合使用的误区!
最新IT新闻:
Silverlight 2 SDK中文版发布
[译稿]微软将 jQuery IntelliSense整合到Visual Studio
微软:不裁员也不削减研发开支
2008年11月22日科技博客精选
诺基亚将支持Lotus Notes 和黑莓争夺市场
公告
我的联系方式:
MSN dannyr@163.com
QQ 563178
20060606新计数器
Detail
<
2004年11月
>
日
一
二
三
四
五
六
31
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
28
29
30
1
2
3
4
5
6
7
8
9
10
11
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
随笔分类
.Net技术(22)
(rss)
C++(9)
(rss)
ColdFusion(5)
(rss)
Delphi(2)
(rss)
DevExpress(1)
(rss)
Flex技术(29)
(rss)
Java(4)
(rss)
Laszlo(9)
(rss)
Spry(9)
(rss)
生活随笔(10)
(rss)
杂项(13)
(rss)
随笔档案
2008年6月 (1)
2008年5月 (2)
2007年9月 (2)
2007年8月 (1)
2007年7月 (3)
2007年1月 (1)
2006年12月 (2)
2006年11月 (1)
2006年10月 (6)
2006年9月 (1)
2006年8月 (4)
2006年7月 (1)
2006年6月 (4)
2006年5月 (4)
2006年4月 (2)
2006年1月 (1)
2005年12月 (1)
2005年11月 (1)
2005年10月 (2)
2005年8月 (1)
2005年7月 (1)
2005年6月 (1)
2005年5月 (1)
2005年4月 (1)
2005年3月 (2)
2005年2月 (1)
2005年1月 (3)
2004年12月 (9)
2004年11月 (9)
2004年10月 (9)
2004年9月 (3)
2004年8月 (2)
2004年7月 (6)
2004年6月 (4)
文章分类
ColdFusion
(rss)
Flex技术(3)
(rss)
文章档案
2004年6月 (3)
我的链接
Trademan
(rss)
www.k-zone.cn
(rss)
呼呼堂
(rss)
牛皮糖
(rss)
最新评论
阅读排行榜
1. 任意对象数组ArrayList的排序法(可自定义排序字段、排序方向)(8842)
2. ASP.NET动态加载用户控件的页面生成过程(8732)
3. 征集比较完善的权限管理方案!(最好有C#方案)(6406)
4. 关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)(5913)
5. Flex2.0文件上传功能(Flex2.0正式版)(5579)
6. Flex RemoteObject 简单应用Demo(5561)
7. 贴个Flex-Jsp-Mysql简单结合例子(5546)
8. DeKlarit:一个不错的top-down CG工具(4665)
9. Rich Client Fashion(转载)+乱弹(4474)
10. Flex2.0实现文件上传功能(服务器为ASP.NET)(4074)
评论排行榜
1. DeKlarit:一个不错的top-down CG工具(24)
2. 关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)(17)
3. Flex2.0实现文件上传功能(服务器为ASP.NET)(17)
4. 如何关闭子线程?征集析构函数与多线程的讨论!(16)
5. 神影无踪-廖添丁FlashGame(15)
6. 征集比较完善的权限管理方案!(最好有C#方案)(13)
7. 贴个Flex-Jsp-Mysql简单结合例子(11)
8. ASP.NET动态加载用户控件的页面生成过程(11)
9. 体验 Delphi2005's ECO II 空间技术