我设计的简单事务控制
其中数据库操作使用了Microsoft.ApplicationBlocks,请参考我的《Microsoft.ApplicationBlocks使用心得》
类结构图:
![](/images/cnblogs_com/dannyr/class.jpg)
相关文件:
接口: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;
![](/Images/OutliningIndicators/None.gif)
using ReceptionPlan.Data.IDAL;
using ReceptionPlan.Data.Utility;
![](/Images/OutliningIndicators/None.gif)
namespace ReceptionPlan.Data.DAL {
public abstract class Manager : IManager {
protected SqlTransaction trans;
protected SqlConnection conn;
protected ArrayList connList;
![](/Images/OutliningIndicators/InBlock.gif)
public Manager() {
connList = new ArrayList();
}
![](/Images/OutliningIndicators/InBlock.gif)
~Manager() {
if (trans != null)
trans.Rollback();
![](/Images/OutliningIndicators/InBlock.gif)
/*
foreach(SqlConnection temp in connList)
{
if(temp !=null && temp.State != ConnectionState.Closed )
temp.Close();
}*/
}
![](/Images/OutliningIndicators/InBlock.gif)
//public SqlConnection Connection
public string Connection {
get {
return Configs.CONN_STR;
/*
SqlConnection temp = new SqlConnection(Configs.CONN_STR);
connList.Add(temp);
return temp;
*/
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public void BeginTransaction() {
conn = new SqlConnection(Configs.CONN_STR);
conn.Open();
trans = conn.BeginTransaction();
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Rollback() {
if (trans != null){
trans.Rollback();
trans = null;
conn.Close();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
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;
}
![](/Images/OutliningIndicators/InBlock.gif)
public int ModifyPlan(Plan plan)
{
if (plan.Id < 0)
return -1;
![](/Images/OutliningIndicators/InBlock.gif)
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;
}
}
}
类结构图:
![](/images/cnblogs_com/dannyr/class.jpg)
相关文件:
接口:IManager.cs IPlanManager.cs
实现:Manager.cs PlanManager.cs
使用:Test.cs
其他相关文件不予说明
Test.cs:测试客户端,可以进行单个功能类内多个方法的事务控制,如果要进行多个功能类的事务控制就需要修改基类Manager中数据库连接的获得方式,目前是简单的获取连接串后直接创建连接,实用时候应该从一个数据库连接工厂类中获得,这样多个Manager类可以共享数据库连接,进行事务控制,并可以有效地管理数据库连接池。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
IManager.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
IPlanManager.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
Manager.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
PlanManager.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)