数据层代码.net

dot net 下的数据处理类

eidolon 2006-03-09

using System;
using RLDMS.Data;
using System.Text;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// 异常处理
/// </summary>
public class RLDMSException:System.Exception{
 public RLDMSException(string ExceptionName):   base(ExceptionName){}
}

/// <summary>
/// 参数类公共接口
/// </summary>
public interface IEntityPublic{}

/// <summary>
/// 具体参数类 实现了System.Collections.IList 接口
/// 每一个参数类都来源于数据库的表
/// Entity_Customers:参数类名
/// Customers:数据表名
/// </summary>
public class CEntity_Customers:System.Collections.IList,IEntityPublic
{
 private System.Collections.ArrayList _List;
 public CEntity_Customers()
 {
  this._List = new System.Collections.ArrayList();
 }

 public bool IsReadOnly
 {
  get{return false;}
 }

 public object this[int index]
 {
  get{return this._List[index];}
  set{this._List[index] = value;}
 }
 public void RemoveAt(int index)
 {
  this._List.Remove(index);
 }
 public void Insert(int index,object value)
 {
  this._List.Insert(index,value);
 }
 public void Remove(object value)
 {
  this._List.Remove(value);
 }
 public bool Contains(object value)
 {
  return this._List.Contains(value);
 }
 public void Clear()
 {
  this._List.Clear();
 }
 public bool IsFixedSize
 {
  get{ return false;}
 }

 public bool IsSynchronized
 {
  get
  {
   return this._List.IsSynchronized;
  }
 }

 public int Count
 {
  get{
   return this._List.Count;
  }
 }

 public void CopyTo(Array array,int index)
 {
  this._List.CopyTo(array,index);
 }

 public object SyncRoot
 {
  get
  {
   return this._List.SyncRoot;
  }
 }

 public System.Collections.IEnumerator GetEnumerator()
 {
  return this._List.GetEnumerator();
 }
 /// <summary>
 /// Collection的add方法的实现
 /// </summary>
 /// <param name="_entity">Entity_customers类型变量</param>
 public int Add( object value)
 {
  return this._List.Add(value);
 }

 public int IndexOf(object value)
 {
  return this._List.IndexOf(value);
  
 }

 /*System.Collections.CollectionBase 的实现
 public void Add( CEntity_Customers _entity)
 {
   List.Add(_entity);
  
 }
 public CEntity_Customers this[int index]
 {
  get
  {
   return (CEntity_Customers) List[index];
  }
 }
 */

 #region 根据数据库字段产生的私有变量和类的属性

 private string _CustomerID = string.Empty;
 private string _CompanyName = string.Empty;
 private string _ContactName = string.Empty;
 private string _ContactTitle = string.Empty;
 private string _Address = string.Empty;
 private string _City = string.Empty;
 private string _Region  = string.Empty;
 private string _PostalCode = string.Empty;
 private string _Country = string.Empty;
 private string _Phone  = string.Empty;
 private string _Fax  = string.Empty;

 public string CustomerID {
  get {
//   if (( _CustomerID == null) || ( _CustomerID == string.Empty ))
//   {
//    CustomerID = string.Empty;    
//   }
   return _CustomerID;
  }
  set{
   _CustomerID = value;
  }
 }

 public string CompanyName 
 {
  get 
  {
   if (( _CompanyName == null) || ( _CompanyName == string.Empty ))
   {
    CompanyName = string.Empty;    
   }
   return _CompanyName;
  }
  set
  {
   _CompanyName = value;
  }
 }

 public string ContactName 
 {
  get 
  {
   if (( _ContactName == null) || ( _ContactName == string.Empty ))
   {
    ContactName = string.Empty;    
   }
   return _ContactName;
  }
  set
  {
   _CompanyName = value;
  }
 }

 public string ContactTitle 
 {
  get 
  {
   if (( _ContactTitle == null) || ( _ContactTitle == string.Empty ))
   {
    ContactTitle = string.Empty;    
   }
   return _ContactTitle;
  }
  set
  {
   _ContactTitle = value;
  }
 }
 
 public string Address 
 {
  get 
  {
   if (( _Address == null) || ( _Address == string.Empty ))
   {
    Address = string.Empty;    
   }
   return _Address;
  }
  set
  {
   _Address = value;
  }
 }

 public string City 
 {
  get 
  {
   if (( _City == null) || ( _City == string.Empty ))
   {
    City = string.Empty;    
   }
   return _City;
  }
  set
  {
   _City = value;
  }
 }

 public string Region 
 {
  get 
  {
   if (( _Region == null) || ( _Region == string.Empty ))
   {
    Region = string.Empty;    
   }
   return _Region;
  }
  set
  {
   _Region = value;
  }
 }

 public string PostalCode 
 {
  get 
  {
   if (( _PostalCode == null) || ( _PostalCode == string.Empty ))
   {
    _PostalCode = string.Empty;    
   }
   return _PostalCode;
  }
  set
  {
   _PostalCode = value;
  }
 }

 public string Country 
 {
  get 
  {
   if (( _Country == null) || ( _Country == string.Empty ))
   {
    Country = string.Empty;    
   }
   return _Country;
  }
  set
  {
   _Country = value;
  }
 }

 public string Phone 
 {
  get 
  {
   if (( _Phone == null) || ( _Phone == string.Empty ))
   {
    Phone = string.Empty;    
   }
   return _Phone;
  }
  set
  {
   _Phone = value;
  }
 }

 public string Fax 
 {
  get 
  {
   if (( _Fax == null) || ( _Fax == string.Empty ))
   {
    Fax = string.Empty;    
   }
   return _Fax;
  }
  set
  {
   _Fax = value;
  }
 }


 #endregion

}


/// <summary>
/// 数据业务类接口
/// </summary>
public interface IDataBusiness
{
 /// <summary>
 /// 判断是否有事务
 /// </summary>
 bool IsTranscation{  get; }
 /// <summary>
 /// 公共属性 父类型 实现时用子类代替
 /// </summary>
 IEntityPublic CEntity{get;set;}
 /// <summary>
 /// 排序语句
 /// </summary>
 string strOrderBy{get;set;}
 /// <summary>
 /// 有事务的增加
 /// </summary>
 /// <param name="DataAccess">数据访问类</param>
 /// <param name="_Transaction">已经存在的事务</param>
 void AddDataWithTranscation( CAccessDataClass DataAccess  ,System.Data.SqlClient.SqlTransaction _Transaction);
 /// <summary>
 /// 没有事务的增加方法
 /// </summary>
 /// <param name="DataAccess">数据访问类</param>
 void AddDataWithOutTranscation(  CAccessDataClass DataAccess);
 /// <summary>
 /// 有事务的修改方法
 /// </summary>
 /// <param name="DataAccess">数据访问类</param>
 /// <param name="_Transaction">已经存在的事务</param>
 void UpdateDataWithTranscation( CAccessDataClass DataAccess  ,System.Data.SqlClient.SqlTransaction _Transaction);
 /// <summary>
 /// 没有事务的修改方法
 /// </summary>
 /// <param name="DataAccess">数据访问类</param>
 void UpdateDataWithOutTranscation( CAccessDataClass DataAccess );
 /// <summary>
 /// 有事务的删除方法
 /// </summary>
 /// <param name="DataAccess"></param>
 /// <param name="_Transaction"></param>
 void DeleteDataWithTranscation( CAccessDataClass DataAccess  ,System.Data.SqlClient.SqlTransaction _Transaction);
 /// <summary>
 /// 没有事务的删除方法
 /// </summary>
 /// <param name="DataAccess"></param>
 void DeleteDataWithOutTranscation( CAccessDataClass DataAccess );
 /// <summary>
 /// 查询方法
 /// </summary>
 /// <param name="DS">DataSet</param>
 void QueryData( CAccessDataClass DataAccess ,out DataSet DS);
 /// <summary>
 /// 查询方法
 /// </summary>
 /// <param name="cOutEntity"></param>
 void QueryData( CAccessDataClass DataAccess ,out IEntityPublic cOutEntity);
 /// <summary>
 /// 返回查询结果,查询语句不带where条件
 /// </summary>
 /// <param name="DataAccess">数据访问类</param>
 /// <param name="strSql">不带条件的查询语句</param>
 /// <returns>字符串</returns>
 string QueryResultWithoutWhere( CAccessDataClass DataAccess ,string strSql);
 /// <summary>
 /// 返回查询结果,查询语句带有where条件
 /// </summary>
 /// <param name="DataAccess">数据访问类</param>
 /// <param name="strSql">带有查询条件的查询语句 </param>
 /// <returns>字符串</returns>
 string QueryResultWithWhere( CAccessDataClass DataAccess ,string strSql);

 /// <summary>
 /// 查询方法
 /// </summary>
 /// <param name="cEntity"></param>
 /// <returns></returns>
 bool IsEntityExsite( CAccessDataClass _DataAccess );

 /// <summary>
 /// 得到查询条件 Where语句
 ///
 /// 或者应用在其它地方
 /// </summary>
 /// <returns>字符串</returns>
 string GetSearchCondition();


 /// <summary>
 /// 根据排序属性得到排序条件
 ///
 /// </summary>
 /// <returns>字符串</returns>
 string GetOrderCondition();
}

 

//业务类制造工厂
public class VehicleFactory
{
 
 public static IDataBusiness CreateBusinessVehicle(Type vehicleType)
 {
  //自己写构造参数!
  System.Reflection.ConstructorInfo construct
   = vehicleType.GetConstructor(Type.EmptyTypes);
  IDataBusiness v = (IDataBusiness)construct.Invoke(null);
  return v;
 }

 public static IEntityPublic CreataEntityVerchicle(Type vehicleType)
 {
  //自己写构造参数!
  System.Reflection.ConstructorInfo construct
   = vehicleType.GetConstructor(Type.EmptyTypes);
  IEntityPublic v = (IEntityPublic)construct.Invoke(null);
  return v;
 }
 

}

 


/// <summary>
/// DataCustomers数据类
/// </summary>
public class DataCustomers :IDataBusiness
{
 private RLDMS.Other.COther Other;
 /// <summary>
 /// SqlCommand参数类
 /// </summary>
 private SqlParameter[] sqlPara ;
 /// <summary>
 /// 事务标志
 /// </summary>
 private bool _IsTranscation;
 /// <summary>
 /// 事务标志
 /// </summary>
 public bool IsTranscation
 {
  get{return _IsTranscation;}
  set{_IsTranscation = value;}
 }
 /// <summary>
 /// 排序条件
 /// </summary>
 private string _strOrderBy = string.Empty;
 /// <summary>
 /// 排序条件
 /// </summary>
 public string strOrderBy
 {
  get{return _strOrderBy;}
  set{_strOrderBy = value;}
 }
 /// <summary>
 /// 实体类Customer
 /// </summary>
 private CEntity_Customers _Entity ;
 /// <summary>
 /// 实体类的公共接口实现
 /// </summary>
 public IEntityPublic CEntity
 {
  get
  {
   if (_Entity == null )
    _Entity = new CEntity_Customers();
   return (IEntityPublic)_Entity;
  }
  set
  {
   _Entity = ( CEntity_Customers)value;
  }
 }

 /// <summary>
 /// 构造函数
 /// </summary>
 public DataCustomers()
 {
  this.Other = new  RLDMS.Other.COther();
  this._Entity = new CEntity_Customers();
 }
 
 /// <summary>
 /// 带有事务的录入接口的实现
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <param name="_Transaction">定义好的事务</param>
 /// <exception cref="SqlException">Sql异常</exception> 
 public void AddDataWithTranscation(  CAccessDataClass _DataAccess , System.Data.SqlClient.SqlTransaction _Transaction)
 {   

  try
  {
   StringBuilder sSql = new StringBuilder();
   sSql = GetInsertSql();
   this.SetParam();
   _DataAccess.Open();
   _DataAccess.ExecuteSQL(_Transaction,CommandType.Text,sSql.ToString(),sqlPara);
  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}

 }
 /// <summary>
 /// 不带事务的录入接口的实现
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <exception cref="SqlException">Sql异常</exception> 
 public void AddDataWithOutTranscation( CAccessDataClass _DataAccess )
 {  
   
  try
  {
   StringBuilder sSql = new StringBuilder();
   sSql = GetInsertSql();
   this.SetParam();
   _DataAccess.Open();
   _DataAccess.ExecuteSQL(CommandType.Text,sSql.ToString(),sqlPara);
  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
    
   
 }

 /// <summary>
 /// 不带事务的修改
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <exception cref="SqlException">Sql异常</exception> 
 public void UpdateDataWithOutTranscation( CAccessDataClass _DataAccess )
 {
  try
  { 
   StringBuilder sSql = new StringBuilder();
   this.SetParam();
   sSql = this.GetUpdateSql();
   _DataAccess.Open();
   _DataAccess.ExecuteSQL(CommandType.Text,sSql.ToString (),sqlPara);

  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }

 /// <summary>
 /// 带事务的修改
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <param name="_Transaction">定义好的事务</param>
 /// <exception cref="SqlException">Sql异常</exception> 
 public void UpdateDataWithTranscation( CAccessDataClass _DataAccess ,System.Data.SqlClient.SqlTransaction _Transaction)
 {
  try
  { 
   StringBuilder sSql = new StringBuilder();
   this.SetParam();
   sSql = this.GetUpdateSql();
   _DataAccess.Open();
   _DataAccess.ExecuteSQL(_Transaction,CommandType.Text,sSql.ToString (),sqlPara);

  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }

 /// <summary>
 /// 带事务的删除
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <param name="_Transaction">定义好的事务</param>
 /// <exception cref="SqlException">Sql异常</exception> 
 public void DeleteDataWithTranscation( CAccessDataClass _DataAccess  ,System.Data.SqlClient.SqlTransaction _Transaction)
 {
  try
  { 
   StringBuilder sSql = new StringBuilder();
   this.SetParam();
   sSql = this.GetDeleteSql();
   _DataAccess.Open();
   _DataAccess.ExecuteSQL(_Transaction,CommandType.Text,sSql.ToString (),sqlPara);

  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }
 /// <summary>
 /// 带有事务的删除
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <exception cref="SqlException">Sql异常</exception> 
 public void  DeleteDataWithOutTranscation( CAccessDataClass _DataAccess )
 {
  try
  { 
   StringBuilder sSql = new StringBuilder();
   this.SetParam();
   sSql = this.GetDeleteSql();
   _DataAccess.Open();
   _DataAccess.ExecuteSQL(CommandType.Text,sSql.ToString (),sqlPara);

  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }

 /// <summary>
 /// 查询方法 返回dataset
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <param name="DS">DataSet</param>
 public void QueryData(  CAccessDataClass _DataAccess  , out DataSet DS)
 {
 
  try
  {
   StringBuilder sSql = new StringBuilder();
   sSql = GetWhereCondition( GetSelectSql() );
   sSql = GetOrderBy( sSql );
   _DataAccess.Open();
   DS = _DataAccess.GetDataSet(System.Data.CommandType.Text,sSql.ToString(),sqlPara);
  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}

 }

 /// <summary>
 /// 查询方法 返回实体类
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <param name="cOutEntity">实体类</param>
 public void QueryData( CAccessDataClass _DataAccess ,out IEntityPublic cOutEntity)
 {
  try
  {
   StringBuilder sSql = new StringBuilder();
   sSql = GetWhereCondition( GetSelectSql() );
   sSql = GetOrderBy( sSql );
   _DataAccess.Open();
   cOutEntity = this.GetEntity(sSql,_DataAccess);
  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }
 /// <summary>
 /// 查询方法 判断数据表中是否存在指定的数据
 /// </summary>
 /// <param name="_DataAccess">数据反问类</param>
 /// <returns>Boolean</returns>
 public bool IsEntityExsite( CAccessDataClass _DataAccess ){
 
  try
  {
   StringBuilder sSql = new StringBuilder();
   sSql = this.GetCountSql();
   this.SetParam();
   sSql.Append( this.GetWhereCondition() );
   _DataAccess.Open();
   int iCount = Convert.ToInt32(_DataAccess.GetScalar(System.Data.CommandType.Text,sSql.ToString(),this.sqlPara)) ;

   if (iCount>0)
    return true;
   else
    return false;
  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }
 
 /// <summary>
 /// 没有Where条件的查询
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <param name="strSql">查询语句</param>
 /// <returns>结果字符串</returns>
 public string QueryResultWithoutWhere(CAccessDataClass _DataAccess ,string strSql)
 {
  try
  {
   StringBuilder sSql = new StringBuilder();
   
   this.SetParam();
   sSql.Append( strSql );
   sSql.Append( this.GetWhereCondition() );
   _DataAccess.Open();
   return _DataAccess.GetScalar(System.Data.CommandType.Text,sSql.ToString(),this.sqlPara) ;
  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }
 /// <summary>
 /// 有Where条件的查询
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <param name="strSql">查询语句</param>
 /// <returns>结果字符串</returns>
 public string QueryResultWithWhere(CAccessDataClass _DataAccess ,string strSql)
 {
  try
  {
   StringBuilder sSql = new StringBuilder();   
   _DataAccess.Open();
   return _DataAccess.GetScalar(System.Data.CommandType.Text,strSql,this.sqlPara) ;
  }
  catch(SqlException ex)
  {
   throw(ex);
  }  
  catch(ApplicationException ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally{ _DataAccess.Close();}
 }

 /// <summary>
 /// 根据参数类的赋值
 /// 返回拼接完好的查询条件
 /// </summary>
 /// <returns></returns>
 public string GetSearchCondition()
 {
  return this.GetWhereCondition().ToString();

 }

 /// <summary>
 /// 根据排序属性的赋值
 /// 返回拼接完好的排序条件
 /// </summary>
 /// <returns></returns>
 public string GetOrderCondition()
 {
  return this.GetOrderBy().ToString();

 }

#region 附属函数 
 /// <summary>
 /// 得到插入语句
 /// </summary>
 /// <returns>返回插入语句</returns>
 private  StringBuilder  GetInsertSql()
 {   

  StringBuilder sSql = new StringBuilder();

  sSql.Append(" INSERT INTO [Customers]");
  sSql.Append("  ( [CustomerID], [CompanyName], [ContactName], [ContactTitle], ");
  sSql.Append("   [Address],  [City],   [Region],  [PostalCode], ");
  sSql.Append("   [Country],  [Phone],  [Fax])");
  sSql.Append(" VALUES ");
  sSql.Append("  ( @CustomerID , @CompanyName, @ContactName, @ContactTitle ");
  sSql.Append("   @Address,  @City,   @Region,  @PostalCode");
  sSql.Append("   @Country,  @Phone,   @Fax");
  sSql.Append(" )");

  return sSql;
 }
 
 /// <summary>
 /// 得到查询语句
 /// </summary>
 /// <returns>返回查询语句</returns>
 private StringBuilder GetSelectSql()
 {
  StringBuilder sSql = new StringBuilder();
   
  sSql.Append(" SELECT  ");
  sSql.Append("   [CustomerID], [CompanyName], [ContactName], [ContactTitle], ");
  sSql.Append("   [Address],  [City],   [Region],  [PostalCode], ");
  sSql.Append("   [Country],  [Phone],  [Fax] ");
  sSql.Append(" FROM [Customers]");

  return sSql;
 }

 /// <summary>
 /// 得到查询结果数量语句
 /// </summary>
 /// <returns>返回结果集数量语句</returns>
 private StringBuilder GetCountSql()
 {
  StringBuilder sSql = new StringBuilder();
   
  sSql.Append(" SELECT count(*) FROM [Customers] ");

  return sSql;

 }

 /// <summary>
 /// 得到删除语句
 /// </summary>
 /// <returns>返回删除语句</returns>
 public StringBuilder GetDeleteSql()
 {
  StringBuilder sSql = new StringBuilder();

  sSql.Append("DELETE FROM [Customers]");
   
  return sSql;
 }  
 /// <summary>
 /// 得到更新语句
 /// </summary>
 /// <returns>返回更新语句</returns>
 private StringBuilder GetUpdateSql()
 {

  StringBuilder sSql = new StringBuilder(),sWhere = new StringBuilder(),sUpdate = new StringBuilder();

  Other.SQLAddUpdate("[Customers]",sUpdate,out sSql);

  sqlPara = this.SetParam();
 
  //Other.SQLAddWhere(" ID ",cEntity.ID," @ID ","=",ref sWhere);
  Other.SQLAddUpdate(" CustomerID ",_Entity.CustomerID," @CustomerID ",ref sUpdate);
  Other.SQLAddUpdate(" CompanyName ",_Entity.CompanyName," @CompanyName ",ref sUpdate);
  Other.SQLAddUpdate(" ContactName ",_Entity.ContactName," @ContactName ",ref sUpdate);
  Other.SQLAddUpdate(" ContactTitle ",_Entity.ContactTitle," @ContactTitle ",ref sUpdate);
  Other.SQLAddUpdate(" Address ",_Entity.Address," @Address ",ref sUpdate);
  Other.SQLAddUpdate(" City ",_Entity.City," @City ",ref sUpdate);
  Other.SQLAddUpdate(" Region ",_Entity.Region," @Region ",ref sUpdate);
  Other.SQLAddUpdate(" PostalCode ",_Entity.PostalCode," @PostalCode ",ref sUpdate);
  Other.SQLAddUpdate(" Country ",_Entity.Country," @Country ",ref sUpdate);
  Other.SQLAddUpdate(" Phone ",_Entity.Phone," @Phone ",ref sUpdate);
  Other.SQLAddUpdate(" Fax ",_Entity.Fax," @Fax ",ref sUpdate);
   
  sSql.Append(sUpdate.ToString());

  Other.SQLAddWhere(ref sSql,sWhere);

  return sSql;
   

 }

 /// <summary>
 /// 得到where语句
 /// </summary>
 /// <returns>返回where语句</returns>
 private StringBuilder GetWhereCondition()
 {
  
  StringBuilder sWhere = new StringBuilder();
  sqlPara = this.SetParam();

  Other.SQLAddWhere(" CustomerID ",_Entity.CustomerID," @CustomerID "," = ",ref sWhere); 
  Other.SQLAddWhere(" CompanyName ",_Entity.CompanyName," @CompanyName "," = ",ref sWhere);
  Other.SQLAddWhere(" ContactName ",_Entity.ContactName," @ContactName "," = ",ref sWhere);
  Other.SQLAddWhere(" ContactTitle ",_Entity.ContactTitle," @ContactTitle "," = ",ref sWhere);
  Other.SQLAddWhere(" Address ",_Entity.Address," @Address "," = ",ref sWhere);
  Other.SQLAddWhere(" City ",_Entity.City," @City "," = ",ref sWhere);
  Other.SQLAddWhere(" Region ",_Entity.Region," @Region "," = ",ref sWhere);
  Other.SQLAddWhere(" PostalCode ",_Entity.PostalCode," @PostalCode "," = ",ref sWhere);
  Other.SQLAddWhere(" Country ",_Entity.Country," @Country "," = ",ref sWhere);
  Other.SQLAddWhere(" Phone ",_Entity.Phone," @Phone "," = ",ref sWhere);
  Other.SQLAddWhere(" Fax ",_Entity.Fax," @Fax "," = ",ref sWhere);
   
  return sWhere;
 }
 /// <summary>
 /// 得到where语句
 /// </summary>
 /// <param name="sSql">查询语句</param>
 /// <returns>返回Select + where语句</returns>
 private StringBuilder GetWhereCondition(StringBuilder sSql)
 {
  
  Other.SQLAddWhere(ref sSql,GetWhereCondition());

  return sSql;
 }
 /// <summary>
 /// 得到排序语句
 /// </summary>
 /// <returns></returns>
 private StringBuilder GetOrderBy()
 {
  StringBuilder sSql = new StringBuilder();
  if (this._strOrderBy == string.Empty)
  {
   return sSql;
  }
  else
  {
   return sSql.Append( " ORDER BY " + this._strOrderBy );
  }

 }
 /// <summary>
 /// 得到排序语句
 /// </summary>
 /// <returns></returns>
 private StringBuilder GetOrderBy(StringBuilder sSql)
 {
  if (this._strOrderBy == string.Empty)
  {
   return sSql;
  }
  else
  {
   return sSql.Append(" ORDER BY " + this._strOrderBy );
  }

 }
 /// <summary>
 /// 得到实体对象
 /// </summary>
 /// <param name="sSql">查询语句</param>
 /// <param name="_DataAccess">数据访问类</param>
 /// <returns>实体对象集合</returns>
 private CEntity_Customers GetEntity( StringBuilder sSql,RLDMS.Data.CAccessDataClass _DataAccess )
 {

  try
  {
   
   
   _DataAccess.Open();

   //cOutEntity = new CEntity_Customers();
   SqlDataReader SDR = _DataAccess.GetReader(CommandType.Text, sSql.ToString(),sqlPara );
    
   #region READ DATA

   while (SDR.Read())
   {
    CEntity_Customers cOutEntity = new CEntity_Customers();

    if (SDR.IsDBNull(0))
    { cOutEntity.CustomerID = ""; }
    else
    { cOutEntity.CustomerID = SDR.GetString(0).ToString(); }
    if (SDR.IsDBNull(1))
    { cOutEntity.CompanyName = ""; }
    else
    { cOutEntity.CompanyName = SDR.GetString(1).ToString(); }
    if (SDR.IsDBNull(2))
    { cOutEntity.ContactName = ""; }
    else
    { cOutEntity.ContactName = SDR.GetString(2).ToString(); }
    if (SDR.IsDBNull(3))
    { cOutEntity.ContactTitle = ""; }
    else
    { cOutEntity.ContactTitle = SDR.GetString(3).ToString(); }
    if (SDR.IsDBNull(4))
    { cOutEntity.Address = ""; }
    else
    { cOutEntity.Address = SDR.GetString(4).ToString(); }
    if (SDR.IsDBNull(5))
    { cOutEntity.City = ""; }
    else
    { cOutEntity.City = SDR.GetString(5).ToString(); }
    if (SDR.IsDBNull(6))
    { cOutEntity.Region = ""; }
    else
    { cOutEntity.Region = SDR.GetString(6).ToString(); }
    if (SDR.IsDBNull(7))
    { cOutEntity.PostalCode = ""; }
    else
    { cOutEntity.PostalCode = SDR.GetString(7).ToString(); }
    if (SDR.IsDBNull(8))
    { cOutEntity.Country = ""; }
    else
    { cOutEntity.Country = SDR.GetString(8).ToString(); }
    if (SDR.IsDBNull(9))
    { cOutEntity.Phone = ""; }
    else
    { cOutEntity.Phone = SDR.GetString(9).ToString(); }
    if (SDR.IsDBNull(10))
    { cOutEntity.Fax = ""; }
    else
    { cOutEntity.Fax = SDR.GetString(10).ToString(); }

    _Entity.Add(cOutEntity);
   
   }  
   #endregion

   return _Entity;
  }
  catch(SqlException ex)
  {
   throw(new Exception(Other.ErrorReplace(ex)));
  }
  finally
  {
   _DataAccess.Close();
  }

  
  
 }

 /// <summary>
 ///数据参数设置
 /// </summary>
 /// <param name="sqlPara">参数对象</param>
 private SqlParameter[] SetParam()
 {
  sqlPara = new SqlParameter[]
   {
    new SqlParameter("@CustomerID",SqlDbType.VarChar),
    new SqlParameter("@CompanyName",SqlDbType.VarChar),
    new SqlParameter("@ContactName",SqlDbType.VarChar),
    new SqlParameter("@ContactTitle",SqlDbType.VarChar),
    new SqlParameter("@Address",SqlDbType.VarChar),
    new SqlParameter("@City",SqlDbType.VarChar),
    new SqlParameter("@Region",SqlDbType.VarChar),
    new SqlParameter("@PostalCode",SqlDbType.VarChar),
    new SqlParameter("@Country",SqlDbType.VarChar),
    new SqlParameter("@Phone",SqlDbType.VarChar),
    new SqlParameter("@Fax",SqlDbType.VarChar)  
   };
   
   Other.SetParm(sqlPara[0],_Entity.CustomerID);
   Other.SetParm(sqlPara[1],_Entity.CompanyName);
   Other.SetParm(sqlPara[2],_Entity.ContactName);
   Other.SetParm(sqlPara[3],_Entity.ContactTitle);
   Other.SetParm(sqlPara[4],_Entity.Address);
   Other.SetParm(sqlPara[5],_Entity.City);
   Other.SetParm(sqlPara[6],_Entity.Region);
   Other.SetParm(sqlPara[7],_Entity.PostalCode);
   Other.SetParm(sqlPara[8],_Entity.Country);
   Other.SetParm(sqlPara[9],_Entity.Phone);
   Other.SetParm(sqlPara[10],_Entity.Fax);

  return sqlPara;

 }

#endregion

}

/// <summary>
/// 数据代理类
/// </summary>
public class DataDelegate
{
 /// <summary>
 /// 数据业务类
 /// </summary>
 private IDataBusiness iDataBusiness;
 /// <summary>
 /// 工厂方法构造函数
 /// 创造IDataBusiness
 /// 调用方法,例:DataContral(typeof(DataCustomers) )
 /// </summary>
 /// <param name="vehicleType"></param>
 public  DataDelegate(Type vehicleType )
 {
  //通过反射建立一个数据操作类
  this.iDataBusiness = VehicleFactory.CreateBusinessVehicle( vehicleType );

 }

 #region 设置、得到查询条件
 /// <summary>
 /// 设置查询条件
 /// </summary>
 /// <param name="_Entity">参数类</param>
 /// <returns>返回拼接好的查询条件</returns>
 public string SetEntity( IEntityPublic _Entity)
 {
  this.iDataBusiness.CEntity = _Entity;
  return iDataBusiness.GetSearchCondition();
 }

 #endregion
 #region 设置、得到排序条件
 /// <summary>
 /// 设置排序条件
 /// </summary>
 /// <param name="strOrderBy">参数类</param>
 /// <returns>返回拼接好的排序条件</returns>
 public string SetOrderBy(string strOrderBy)
 {
  this.iDataBusiness.strOrderBy = strOrderBy;
  return iDataBusiness.GetOrderCondition();
 }

 
 #endregion
 #region 数据增加
 /// <summary>
 /// 数据增加
 /// 如果对于复杂业务在一个事务下要执行多个步骤
 /// 需要仿照此方法重写
 /// </summary>
 /// <returns></returns>
 public bool AddData()
 {
  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();
  System.Data.SqlClient.SqlTransaction _SqlTranscation = DataAccess.BeginTransaction("Transcation1");

  if ( iDataBusiness.IsTranscation )
  {

   try
   { 
    DataAccess.Open();    
    iDataBusiness.AddDataWithTranscation(DataAccess, _SqlTranscation);
    _SqlTranscation.Commit();
    return true;
   }
   catch(SqlException ex)
   {
    _SqlTranscation.Rollback("Transcation1");
    return false;
   }
   catch(RLDMSException ex)
   {
    throw(ex);
   }
   finally
   {
    DataAccess.Close();
   }
  }
  else
  {
   try
   {
    DataAccess.Open();
    iDataBusiness.AddDataWithOutTranscation(DataAccess);
    return true;
   
   }
   catch(SqlException ex)
   {
    throw( new RLDMSException( ex.Message ));
    return false;
   }
   finally{ DataAccess.Close();}

   
  }
 }

 #endregion
 #region 数据删除
 /// <summary>
 /// 数据删除
 /// 如果对于复杂业务在一个事务下要执行多个步骤
 /// 需要仿照此方法重写
 /// </summary>
 /// <returns>Boolean</returns>
 public bool DeleteData()
 {
  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();
  System.Data.SqlClient.SqlTransaction _SqlTranscation = DataAccess.BeginTransaction("Transcation1");

  if ( iDataBusiness.IsTranscation )
  {

   try
   { 
    DataAccess.Open();    
    iDataBusiness.DeleteDataWithTranscation(DataAccess, _SqlTranscation);
    _SqlTranscation.Commit();
    return true;
   }
   catch(SqlException ex)
   {
    _SqlTranscation.Rollback("Transcation1");
    return false;
   }
   catch(RLDMSException ex)
   {
    throw(ex);
   }
   finally
   {
    DataAccess.Close();
   }
  }
  else
  {
   try
   {
    DataAccess.Open();
    iDataBusiness.DeleteDataWithOutTranscation(DataAccess);
    return true;
   
   }
   catch(SqlException ex)
   {
    throw( new RLDMSException( ex.Message ));
    return false;
   }
   finally{ DataAccess.Close();}

   
  }

 }

 #endregion
 #region 数据修改
 /// <summary>
 /// 数据修改
 /// 如果对于复杂业务在一个事务下要执行多个步骤
 /// 需要仿照此方法重写
 /// </summary>
 /// <returns>Boolean</returns>
 public bool UpdateData()
 {
  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();
  System.Data.SqlClient.SqlTransaction _SqlTranscation = DataAccess.BeginTransaction("Transcation1");

  if ( iDataBusiness.IsTranscation )
  {

   try
   { 
    DataAccess.Open();    
    iDataBusiness.UpdateDataWithTranscation(DataAccess, _SqlTranscation);
    _SqlTranscation.Commit();
    return true;
   }
   catch(SqlException ex)
   {
    _SqlTranscation.Rollback("Transcation1");
    return false;
   }
   catch(RLDMSException ex)
   {
    throw(ex);
   }
   finally
   {
    DataAccess.Close();
   }
  }
  else
  {
   try
   {
    DataAccess.Open();
    iDataBusiness.UpdateDataWithOutTranscation(DataAccess);
    return true;
   
   }
   catch(SqlException ex)
   {
    throw( new RLDMSException( ex.Message ));
    return false;
   }
   finally{ DataAccess.Close();}

   
  }

 }

 #endregion
 #region 数据查询
 
 /// <summary>
 /// 数据查询
 /// </summary>
 /// <param name="DS">DataSet</param>
 public DataSet QueryDataSet()
 {
  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();
  try
  { 
   DataAccess.Open(); 
   DataSet DS;
   iDataBusiness.QueryData( DataAccess, out DS);
   return DS;
  }
  catch(SqlException ex)
  {    
   throw( new RLDMSException(ex.Message));
  }
  catch(RLDMSException ex)
  {
   throw(ex);
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally
  {
   DataAccess.Close();
  }
  

 }

 /// <summary>
 /// 查询方法
 /// </summary>
 /// <param name="cOutEntity"></param>
 public IEntityPublic QueryDataCollections( )
 {
  IEntityPublic cOutEntity ;

  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();

  try
  { 
   DataAccess.Open();    
   iDataBusiness.QueryData( DataAccess, out cOutEntity);
   
  }
  catch(SqlException ex)
  {    
   throw( new RLDMSException(ex.Message));
  }
  catch(RLDMSException ex)
  {
   throw(ex);
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally
  {
   DataAccess.Close();   
  }
  return cOutEntity;
 }

 
 /// <summary>
 /// 查询方法
 /// </summary>
 /// <param name="_DataAccess">数据访问类</param>
 /// <returns>Boolean</returns>
 public bool IsEntityExsite( CAccessDataClass _DataAccess )
 {
  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();

  try
  { 
   DataAccess.Open();    
   return iDataBusiness.IsEntityExsite( _DataAccess);
   
  }
  catch(SqlException ex)
  {    
   throw( new RLDMSException(ex.Message));
  }
  catch(RLDMSException ex)
  {
   throw(ex);
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally
  {
   DataAccess.Close();
  }

 }

 /// <summary>
 /// 返回查询结果
 /// </summary>
 /// <param name="strSql"></param>
 /// <returns></returns>
 public string QueryResultWhithWhere( string strSql)
 {
  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();
  try
  { 
   DataAccess.Open();    
   return iDataBusiness.QueryResultWithWhere( DataAccess, strSql );
  }
  catch(SqlException ex)
  {    
   throw( new RLDMSException(ex.Message));
  }
  catch(RLDMSException ex)
  {
   throw(ex);
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally
  {
   DataAccess.Close();
  }
  

 }


 /// <summary>
 /// 返回查询结果
 /// </summary>
 /// <param name="strSql"></param>
 /// <returns></returns>
 public string QueryResultWhithOutWhere( string strSql)
 {
  //定义数据访问类
  CAccessDataClass DataAccess= new CAccessDataClass ();
  try
  { 
   DataAccess.Open();    
   return iDataBusiness.QueryResultWithoutWhere( DataAccess,strSql );
  }
  catch(SqlException ex)
  {    
   throw( new RLDMSException(ex.Message));
  }
  catch(RLDMSException ex)
  {
   throw(ex);
  }
  catch(Exception ex)
  {
   throw(new RLDMSException(ex.Message));
  }
  finally
  {
   DataAccess.Close();
  }
  
 }
 #endregion
}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值