其中数据库操作使用了Microsoft.ApplicationBlocks,请参考我的《Microsoft.ApplicationBlocks使用心得》
类结构图:
class.jpg
相关文件:
接口:IManager.cs IPlanManager.cs
实现:Manager.cs PlanManager.cs
使用:Test.cs
其他相关文件不予说明
Test.cs:测试客户端,可以进行单个功能类内多个方法的事务控制,如果要进行多个功能类的事务控制就需要修改基类Manager中数据库连接的获得方式,目前是简单的获取连接串后直接创建连接,实用时候应该从一个数据库连接工厂类中获得,这样多个Manager类可以共享数据库连接,进行事务控制,并可以有效地管理数据库连接池。
None.gif
using
 System;
None.gif
using
 System.Collections;
None.gif
using
 ReceptionPlan.Data.Components;
None.gif
using
 ReceptionPlan.Data.IDAL;
None.gif
using
 ReceptionPlan.Data.DAL;
None.gif
namespace
 ReceptionPlan.Data.Services
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ReceptionPlan 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public class ReceptionPlanService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ReceptionPlanService()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加计划
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="plan">计划对象</param>
ExpandedSubBlockEnd.gif        
/// <returns>计划ID</returns>
InBlock.gif        public static int AddPlan(Plan plan)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IPlanManager pm 
= new PlanManager();
InBlock.gif            pm.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int id = pm.AddPlan(plan);
InBlock.gif                plan.Id 
= id;
InBlock.gif                plan.Name 
= "XXXX";
InBlock.gif                pm.ModifyPlan(plan)
InBlock.gif                pm.Commit();
InBlock.gif                
return id;
ExpandedSubBlockEnd.gif            }
InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pm.Rollback();
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif}
InBlock.gif
IManager.cs
None.gif
using
 System;
None.gif
namespace
 ReceptionPlan.Data.IDAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// IManager 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public interface IManager
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
void BeginTransaction();
InBlock.gif        
void Rollback();
InBlock.gif        
void Commit();
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif
IPlanManager.cs
None.gif
using
 System;
None.gif
using
 System.Collections;
None.gif
using
 ReceptionPlan.Data.Components;
None.gif
namespace
 ReceptionPlan.Data.IDAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// IPlanManager 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public interface IPlanManager : IManager
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int AddPlan(Plan plan);
InBlock.gif        
int ModifyPlan(Plan plan);
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif
Manager.cs
None.gif
using
 System;
None.gif
using
 System.Data;
None.gif
using
 System.Data.SqlClient;
None.gif
using
 System.Collections;
None.gif
None.gif
using
 ReceptionPlan.Data.IDAL;
None.gif
using
 ReceptionPlan.Data.Utility;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace
 ReceptionPlan.Data.DAL 
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public abstract class Manager : IManager dot.gif{
InBlock.gif        
protected SqlTransaction trans;
InBlock.gif        
protected SqlConnection conn;
InBlock.gif        
protected ArrayList connList;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Manager() dot.gif{
InBlock.gif            connList 
= new ArrayList();
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
~Manager() dot.gif{
InBlock.gif            
if (trans != null)
InBlock.gif                trans.Rollback();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*
InBlock.gif            foreach(SqlConnection temp in connList)
InBlock.gif            {
InBlock.gif                if(temp !=null && temp.State != ConnectionState.Closed  )
InBlock.gif                    temp.Close();
ExpandedSubBlockEnd.gif            }
*/
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
//public SqlConnection Connection
ExpandedSubBlockStart.gifContractedSubBlock.gif
        public string Connection dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{
InBlock.gif                
return Configs.CONN_STR;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**//*
InBlock.gif                SqlConnection temp = new SqlConnection(Configs.CONN_STR);
InBlock.gif                connList.Add(temp);
InBlock.gif                return temp;
ExpandedSubBlockEnd.gif                
*/
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void BeginTransaction() dot.gif{
InBlock.gif            conn 
= new SqlConnection(Configs.CONN_STR);
InBlock.gif            conn.Open();
InBlock.gif            trans 
= conn.BeginTransaction();
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void Rollback() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (trans != null)dot.gif{                
InBlock.gif                trans.Rollback();
InBlock.gif                trans 
= null;
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void Commit() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (trans != null)dot.gif{
InBlock.gif                trans.Commit();
InBlock.gif                trans 
= null;
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif
PlanManager.cs
None.gif
using
 System;
None.gif
using
 System.Collections;
None.gif
using
 Microsoft.ApplicationBlocks.Data;
None.gif
using
 System.Data;
None.gif
using
 System.Data.SqlClient;
None.gif
using
 ReceptionPlan.Data.Components;
None.gif
using
 ReceptionPlan.Data.IDAL;
None.gif
using
 ReceptionPlan.Data.Utility;
None.gif
namespace
 ReceptionPlan.Data.DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// PlanManager 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public class PlanManager : Manager, IPlanManager
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
public PlanManager()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }
InBlock.gif        
public int AddPlan(Plan plan)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int id = -1;
InBlock.gif            
string sql = string.Empty;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sql 
= string.Format("insert into [RP_Plan] XXXX);
InBlock.gif
                object AID = SqlHelper.ExecuteScalar(trans, CommandType.Text, sql );
InBlock.gif                id 
= Convert.ToInt32(AID);
ExpandedSubBlockEnd.gif            }
InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception(ex.ToString() + "\n sql=" + sql);
ExpandedSubBlockEnd.gif            }
InBlock.gif            
return id;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public int ModifyPlan(Plan plan)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (plan.Id < 0)
InBlock.gif                
return -1;
InBlock.gif
InBlock.gif            
string sql = string.Empty;
InBlock.gif            
int ret = -1;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sql 
= string.Format("update RP_Plan  XXXX);
InBlock.gif
                ret = SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql );
ExpandedSubBlockEnd.gif            }
InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception(ex.ToString() + "\n sql=" + sql);
ExpandedSubBlockEnd.gif            }
InBlock.gif            
return ret;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}