PageRenderTime 49ms CodeModel.GetById 22ms app.highlight 22ms RepoModel.GetById 0ms app.codeStats 0ms

/EQT_V1/EQT.DAL/DaDept.auto.cs

http://sgsoft-las.googlecode.com/
C# | 1034 lines | 815 code | 86 blank | 133 comment | 26 complexity | 746d211cab7877060fe5693518887d14 MD5 | raw file
Possible License(s): LGPL-2.1
   1#region DaDept
   2/*----------------------------------------------------------------
   3// ????DaDept.cs
   4// ??????Dept??????
   5//
   6// 
   7// ?????2009-02-27
   8//
   9// ????? 
  10// ????? 
  11//----------------------------------------------------------------*/
  12using System;
  13using System.Collections;
  14using System.Collections.Generic;
  15using System.Data;
  16using System.Globalization;
  17using System.Text;
  18using System.Xml;
  19using System.Data.Common;
  20using Microsoft.Practices.EnterpriseLibrary.Data;
  21using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
  22//using Microsoft.Practices.EnterpriseLibrary.Logging;
  23using EQT.Model; 
  24//using IDataLayer;
  25
  26namespace EQT.Dal
  27{
  28    public	partial  class DaDept   //?????????,IDept 
  29    {   
  30	
  31		//???????????????????
  32		//???????????????SQL Server??????????????????ID??????entiry?
  33		//??????????????????
  34		#region SQL const
  35		private const  string SQL_INSERT="INSERT INTO Dept (Dept_ID ,Parent_Dept_ID ,Dept_Name ,Dept_Tel ,Dept_Manager_Code ,Rem ) VALUES "
  36			+"(@Dept_Id,@Parent_Dept_ID,@Dept_Name,@Dept_Tel,@Dept_Manager_Code,@Rem)";
  37		private const string SQL_UPDATE="UPDATE Dept SET Parent_Dept_ID=@Parent_Dept_ID, Dept_Name=@Dept_Name, Dept_Tel=@Dept_Tel, Dept_Manager_Code=@Dept_Manager_Code, Rem=@Rem WHERE Dept_ID=@Dept_ID";
  38		private const string SQL_DELETE="DELETE FROM Dept WHERE Dept_ID=@Dept_ID";
  39		private const string SQL_SELECT="SELECT Dept_ID ,Parent_Dept_ID ,Dept_Name ,Dept_Tel ,Dept_Manager_Code ,Rem  from Dept  ";
  40		private const string SQL_SELECT_ONE="SELECT Dept_ID ,Parent_Dept_ID ,Dept_Name ,Dept_Tel ,Dept_Manager_Code ,Rem  from Dept  WHERE Dept_ID=@Dept_ID";                              
  41		#endregion
  42		
  43        private Database db;
  44		
  45		#region Constructor
  46	 
  47		/// <summary>
  48        /// ???????????????????????
  49        /// </summary>
  50		public DaDept ()
  51		{ 
  52			db=DatabaseFactory.CreateDatabase();
  53		}
  54		/// <summary>
  55        /// ?????databaseName??????
  56        /// </summary>
  57        /// <param name="databaseName">??????????????</param>
  58		public DaDept (string databaseName)
  59		{ 
  60            this.db = DatabaseFactory.CreateDatabase(databaseName);
  61		} 
  62		
  63		public DaDept (Database db)
  64		{ 
  65            this.db = db;
  66		} 
  67		
  68		#endregion
  69		
  70		//???CRUD??????????????????
  71
  72	    #region Add
  73		
  74		///build the command object.It never throw exception.
  75		private DbCommand ConstructAddCommand()
  76		{
  77			DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
  78			 
  79			//????:Dept_Id	
  80			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString);
  81			//????:Parent_Dept_ID	
  82			db.AddInParameter(dbCommand,"Parent_Dept_ID",DbType.AnsiString);
  83			//????:Dept_Name	
  84			db.AddInParameter(dbCommand,"Dept_Name",DbType.String);
  85			//????:Dept_Tel	
  86			db.AddInParameter(dbCommand,"Dept_Tel",DbType.AnsiString);
  87			//????:Dept_Manager_Code	
  88			db.AddInParameter(dbCommand,"Dept_Manager_Code",DbType.AnsiString);
  89			//????:Rem	
  90			db.AddInParameter(dbCommand,"Rem",DbType.String);
  91			return dbCommand;
  92		}		
  93		private DbCommand ConstructAddCommand(MoDept entity)
  94		{
  95			DbCommand dbCommand=db.GetSqlStringCommand(SQL_INSERT);
  96			 
  97			//????:Dept_Id	
  98			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,entity.Dept_Id);
  99			//????:Parent_Dept_ID	
 100			db.AddInParameter(dbCommand,"Parent_Dept_ID",DbType.AnsiString,entity.Parent_Dept_Id);
 101			//????:Dept_Name	
 102			db.AddInParameter(dbCommand,"Dept_Name",DbType.String,entity.Dept_Name);
 103			//????:Dept_Tel	
 104			db.AddInParameter(dbCommand,"Dept_Tel",DbType.AnsiString,entity.Dept_Tel);
 105			//????:Dept_Manager_Code	
 106			db.AddInParameter(dbCommand,"Dept_Manager_Code",DbType.AnsiString,entity.Dept_Manager_Code);
 107			//????:Rem	
 108			db.AddInParameter(dbCommand,"Rem",DbType.String,entity.Rem);
 109			 
 110			return dbCommand;
 111		}
 112	
 113	    /// <summary>
 114		///????????????????commit
 115		/// </summary>
 116	    public int Add(MoDept entity,DbTransaction trans)
 117	    {	try
 118            {
 119				DbCommand dbCommand=ConstructAddCommand(entity);			
 120				return db.ExecuteNonQuery(dbCommand,trans);
 121			}
 122			catch(DbException ex)
 123			{				
 124				HandleDbException(ex,entity);				
 125			}
 126			return 0;
 127			
 128	    }		
 129	    public int Add(MoDept entity) 
 130        {	
 131			try
 132            {
 133				DbCommand dbCommand=ConstructAddCommand(entity);			
 134				return db.ExecuteNonQuery(dbCommand);
 135			}
 136			catch(DbException ex)
 137			{				
 138				HandleDbException(ex,entity);		
 139			}
 140			return 0;
 141	    }
 142		
 143		public int Add(List<MoDept> entity,DbTransaction trans)
 144		{
 145			int rc=0;		
 146            DbCommand dbCommand=ConstructAddCommand();
 147			int j=0;
 148			try
 149			{
 150			   for(j=0;j<entity.Count;j++)
 151			   {	
 152				//????:Dept_Id	
 153				db.SetParameterValue(dbCommand,"Dept_Id",entity[j].Dept_Id);
 154				//????:Parent_Dept_ID	
 155				db.SetParameterValue(dbCommand,"Parent_Dept_ID",entity[j].Parent_Dept_Id);
 156				//????:Dept_Name	
 157				db.SetParameterValue(dbCommand,"Dept_Name",entity[j].Dept_Name);
 158				//????:Dept_Tel	
 159				db.SetParameterValue(dbCommand,"Dept_Tel",entity[j].Dept_Tel);
 160				//????:Dept_Manager_Code	
 161				db.SetParameterValue(dbCommand,"Dept_Manager_Code",entity[j].Dept_Manager_Code);
 162				//????:Rem	
 163				db.SetParameterValue(dbCommand,"Rem",entity[j].Rem);
 164				rc+=db.ExecuteNonQuery(dbCommand,trans);
 165			   }
 166			}
 167			catch(DbException ex)
 168			{
 169				HandleDbException(ex,entity[j]);				
 170			}			 
 171            return rc;
 172		}		
 173		public int Add(List<MoDept> entity)
 174		{
 175			int rc = 0;			
 176			using (DbConnection cnn = db.CreateConnection())
 177            {    
 178				DbTransaction trans=null;
 179				try
 180				{
 181					cnn.Open();
 182					using(trans = cnn.BeginTransaction())
 183					{
 184						rc=Add(entity,trans);//This function throw customer exception.
 185						trans.Commit();
 186					}
 187					cnn.Close();
 188				}
 189				catch(DbException ex)
 190				{
 191					if(trans!=null)trans.Rollback();		
 192					HandleDbException(ex);	
 193				}
 194				///TODO:catch customer exception.
 195				//catch
 196				//{
 197				//	
 198				//}
 199			}
 200			return rc;
 201		}
 202		
 203        public int Add(DataTable dt, DbTransaction trans)
 204		{
 205			int rc=0;		 
 206            DbCommand dbCommand =ConstructAddCommand();	
 207			int j=0;
 208			try
 209			{
 210			   for(j=0;j<dt.Rows.Count;j++)
 211			   {	
 212					//????:Dept_Id	
 213					db.SetParameterValue(dbCommand,"Dept_Id",dt.Rows[j]["Dept_Id"]);
 214					//????:Parent_Dept_ID	
 215					db.SetParameterValue(dbCommand,"Parent_Dept_ID",dt.Rows[j]["Parent_Dept_ID"]);
 216					//????:Dept_Name	
 217					db.SetParameterValue(dbCommand,"Dept_Name",dt.Rows[j]["Dept_Name"]);
 218					//????:Dept_Tel	
 219					db.SetParameterValue(dbCommand,"Dept_Tel",dt.Rows[j]["Dept_Tel"]);
 220					//????:Dept_Manager_Code	
 221					db.SetParameterValue(dbCommand,"Dept_Manager_Code",dt.Rows[j]["Dept_Manager_Code"]);
 222					//????:Rem	
 223					db.SetParameterValue(dbCommand,"Rem",dt.Rows[j]["Rem"]);
 224					rc+=db.ExecuteNonQuery(dbCommand,trans);
 225			   }
 226			}
 227			catch(DbException ex)
 228			{				
 229				HandleDbException(ex,dt.Rows[j]);				
 230			}	
 231			 
 232            return rc;
 233		}		
 234		public int Add(DataTable dt)
 235        {
 236			int rc=0;
 237            using (DbConnection cnn = db.CreateConnection())
 238            { 
 239				DbTransaction trans=null;
 240				try
 241                {
 242					cnn.Open();
 243					using(trans = cnn.BeginTransaction())
 244					{
 245						rc = Add(dt, trans);
 246						trans.Commit();
 247						cnn.Close();
 248					}
 249				}
 250				catch(DbException ex)
 251				{	
 252					if(trans!=null)trans.Rollback();
 253					HandleDbException(ex);	
 254				}	
 255				///TODO:catch customer exception.
 256				//catch
 257				//{
 258				//	
 259				//}
 260            }
 261            return rc;
 262        }
 263		#endregion
 264		
 265		#region Update
 266		private DbCommand ConstructUpdateCommand()
 267		{
 268			DbCommand dbCommand=db.GetSqlStringCommand(SQL_UPDATE);
 269		    
 270			db.AddInParameter(dbCommand,"Parent_Dept_ID",DbType.AnsiString);
 271			db.AddInParameter(dbCommand,"Dept_Name",DbType.String);
 272			db.AddInParameter(dbCommand,"Dept_Tel",DbType.AnsiString);
 273			db.AddInParameter(dbCommand,"Dept_Manager_Code",DbType.AnsiString);
 274			db.AddInParameter(dbCommand,"Rem",DbType.String);
 275		 
 276			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString);
 277			 
 278			return dbCommand;
 279		}
 280		private DbCommand ConstructUpdateCommand(MoDept entity)
 281		{
 282			DbCommand dbCommand=db.GetSqlStringCommand(SQL_UPDATE);
 283		    
 284			db.AddInParameter(dbCommand,"Parent_Dept_ID",DbType.AnsiString,entity.Parent_Dept_Id);
 285			db.AddInParameter(dbCommand,"Dept_Name",DbType.String,entity.Dept_Name);
 286			db.AddInParameter(dbCommand,"Dept_Tel",DbType.AnsiString,entity.Dept_Tel);
 287			db.AddInParameter(dbCommand,"Dept_Manager_Code",DbType.AnsiString,entity.Dept_Manager_Code);
 288			db.AddInParameter(dbCommand,"Rem",DbType.String,entity.Rem);
 289		 
 290			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,entity.Dept_Id);
 291			 
 292			return dbCommand;
 293		}
 294		/// <summary>
 295		/// ???????????:??????????
 296		/// </summary>	
 297	    public int Update(MoDept entity,DbTransaction trans)
 298	    {	    			
 299		    try
 300            {			
 301				DbCommand dbCommand=ConstructUpdateCommand(entity);
 302				return db.ExecuteNonQuery(dbCommand,trans);
 303			}
 304			catch(DbException ex)
 305			{				
 306				HandleDbException(ex,entity);		
 307			}
 308			return 0;
 309		}		
 310		public int Update(MoDept entity)
 311		{
 312			try
 313            {			
 314				DbCommand dbCommand=ConstructUpdateCommand(entity);
 315				return db.ExecuteNonQuery(dbCommand);
 316			}
 317			catch(DbException ex)
 318			{				
 319				HandleDbException(ex,entity);		
 320			}
 321			return 0;			
 322		}		
 323		public int Update(List<MoDept> entity,DbTransaction trans)
 324		{
 325			int rc=0;			 
 326            DbCommand dbCommand=ConstructUpdateCommand();
 327			int j=0;
 328			try
 329			{
 330			    for(j=0;j<entity.Count;j++)
 331				{	
 332					db.SetParameterValue(dbCommand,"Dept_Id",entity[j].Dept_Id);
 333					db.SetParameterValue(dbCommand,"Parent_Dept_ID",entity[j].Parent_Dept_Id);
 334					db.SetParameterValue(dbCommand,"Dept_Name",entity[j].Dept_Name);
 335					db.SetParameterValue(dbCommand,"Dept_Tel",entity[j].Dept_Tel);
 336					db.SetParameterValue(dbCommand,"Dept_Manager_Code",entity[j].Dept_Manager_Code);
 337					db.SetParameterValue(dbCommand,"Rem",entity[j].Rem);
 338					rc+=db.ExecuteNonQuery(dbCommand,trans);
 339				}
 340			}
 341			catch(DbException ex)
 342			{				
 343				HandleDbException(ex,entity[j]);		
 344			}			 
 345			return rc;			
 346		}		
 347		public int Update(List<MoDept> entity)
 348		{
 349			int rc = 0;	
 350			using (DbConnection cnn = db.CreateConnection())
 351            {   
 352				DbTransaction trans=null;
 353				try
 354				{          
 355					cnn.Open();
 356					using(trans = cnn.BeginTransaction())
 357					{
 358					rc=Update(entity,trans);
 359					trans.Commit();		
 360					}
 361					cnn.Close();
 362				}
 363				catch(DbException ex)
 364				{
 365					if(trans!=null)trans.Rollback();	
 366					HandleDbException(ex);	
 367				}				
 368			}
 369			return rc;
 370		}		
 371		public int Update(DataTable dt, DbTransaction trans)
 372		{
 373			int rc=0;			 
 374            DbCommand dbCommand=ConstructUpdateCommand();		
 375			int j=0;
 376		    try
 377		    {
 378			    for(j=0;j<dt.Rows.Count;j++)
 379				{	
 380					db.SetParameterValue(dbCommand,"Dept_Id",dt.Rows[j]["Dept_Id"]);
 381					db.SetParameterValue(dbCommand,"Parent_Dept_ID",dt.Rows[j]["Parent_Dept_ID"]);
 382					db.SetParameterValue(dbCommand,"Dept_Name",dt.Rows[j]["Dept_Name"]);
 383					db.SetParameterValue(dbCommand,"Dept_Tel",dt.Rows[j]["Dept_Tel"]);
 384					db.SetParameterValue(dbCommand,"Dept_Manager_Code",dt.Rows[j]["Dept_Manager_Code"]);
 385					db.SetParameterValue(dbCommand,"Rem",dt.Rows[j]["Rem"]);
 386					rc+=db.ExecuteNonQuery(dbCommand,trans);
 387				}
 388			}
 389			catch(DbException ex)
 390			{				
 391				HandleDbException(ex,dt.Rows[j]);		
 392			}		
 393            return rc;
 394			
 395		}		
 396		public int Update(DataTable dt)
 397        {
 398			if(dt==null ||dt.Rows.Count==0) return 0;
 399            int rc=0;       
 400            using (DbConnection cnn = db.CreateConnection())
 401            {
 402				DbTransaction trans=null;
 403				try
 404                {
 405					cnn.Open();
 406					using(trans = cnn.BeginTransaction())
 407					{
 408						rc = Add(dt, trans);
 409						trans.Commit();						
 410					}   
 411					cnn.Close();                  
 412                }
 413                catch(DbException ex)
 414				{
 415					if(trans!=null)trans.Rollback();	
 416					HandleDbException(ex);	
 417				}		
 418                
 419            }
 420            return rc;
 421        }
 422		#endregion
 423		
 424		#region Update By DataSet
 425		private void PrepareDataAdapterCommand(out DbCommand dbInsertCommand,
 426		                     out DbCommand dbUpdateCommand,out DbCommand dbDeleteCommand)
 427		{
 428			dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
 429			#region set insert cmd parameters
 430			db.AddInParameter(dbInsertCommand, "Dept_Id",DbType.AnsiString, "Dept_Id", DataRowVersion.Current);
 431			db.AddInParameter(dbInsertCommand, "Parent_Dept_ID",DbType.AnsiString, "Parent_Dept_ID", DataRowVersion.Current);
 432			db.AddInParameter(dbInsertCommand, "Dept_Name",DbType.String, "Dept_Name", DataRowVersion.Current);
 433			db.AddInParameter(dbInsertCommand, "Dept_Tel",DbType.AnsiString, "Dept_Tel", DataRowVersion.Current);
 434			db.AddInParameter(dbInsertCommand, "Dept_Manager_Code",DbType.AnsiString, "Dept_Manager_Code", DataRowVersion.Current);
 435			db.AddInParameter(dbInsertCommand, "Rem",DbType.String, "Rem", DataRowVersion.Current);
 436			#endregion
 437			dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
 438			#region Set update cmd value parameters
 439			db.AddInParameter(dbUpdateCommand, "Parent_Dept_ID", DbType.AnsiString, "Parent_Dept_ID", DataRowVersion.Current);
 440			db.AddInParameter(dbUpdateCommand, "Dept_Name", DbType.String, "Dept_Name", DataRowVersion.Current);
 441			db.AddInParameter(dbUpdateCommand, "Dept_Tel", DbType.AnsiString, "Dept_Tel", DataRowVersion.Current);
 442			db.AddInParameter(dbUpdateCommand, "Dept_Manager_Code", DbType.AnsiString, "Dept_Manager_Code", DataRowVersion.Current);
 443			db.AddInParameter(dbUpdateCommand, "Rem", DbType.String, "Rem", DataRowVersion.Current);
 444			#endregion
 445			#region set update cmd pk where parameters
 446			db.AddInParameter(dbUpdateCommand, "Dept_Id", DbType.AnsiString, "Dept_Id", DataRowVersion.Current);
 447			#endregion			
 448			dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
 449			#region set delete cmd pk where parameters
 450			db.AddInParameter(dbDeleteCommand, "Dept_Id", DbType.AnsiString, "Dept_Id", DataRowVersion.Current);
 451			#endregion
 452		}
 453		
 454		
 455		
 456		/// <summary>
 457		/// ??DataSet??????????
 458		/// </summary>
 459		public int UpdateByDataSet(DataSet dst, string strTableName, DbTransaction trans)
 460		{
 461			DbCommand dbInsertCommand = null;			
 462			DbCommand dbUpdateCommand = null;
 463			DbCommand dbDeleteCommand = null;
 464			PrepareDataAdapterCommand(out dbInsertCommand,out dbUpdateCommand,out dbDeleteCommand);
 465			try
 466            {
 467			    return db.UpdateDataSet(dst, strTableName, dbInsertCommand, dbUpdateCommand, dbDeleteCommand, trans);
 468			}
 469			catch(DbException ex)
 470			{			
 471				trans.Rollback();
 472				HandleDbException(ex);				
 473			}
 474			return 0;//TODO:Should return Application Specific Error Code.
 475		}
 476		
 477		/// <summary>
 478		/// ??DataSet???????????
 479		/// </summary>
 480		public int UpdateByDataSet(DataSet dst, string strTableName)
 481		{
 482			DbCommand dbInsertCommand = null;			
 483			DbCommand dbUpdateCommand = null;
 484			DbCommand dbDeleteCommand = null;
 485			PrepareDataAdapterCommand(out dbInsertCommand,out dbUpdateCommand,out dbDeleteCommand);
 486			try
 487            {
 488			    return db.UpdateDataSet(dst, strTableName, dbInsertCommand, dbUpdateCommand, dbDeleteCommand, UpdateBehavior.Transactional);			
 489			}
 490			catch(DbException ex)
 491			{	
 492				HandleDbException(ex);				
 493			}
 494			return 0;//TODO:Should return Application Specific Error Code.			
 495		}
 496		
 497		
 498		
 499		#endregion 
 500		
 501		#region Delete
 502		
 503		private DbCommand ConstructDeleteCommand()
 504		{
 505			DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
 506			 
 507			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString);
 508			 
 509			return dbCommand;
 510		}		
 511		private DbCommand ConstructDeleteCommand(MoDept entity)
 512		{
 513			DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
 514			 
 515			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,entity.Dept_Id);
 516		 
 517			return dbCommand;
 518		}
 519		
 520		/// <summary>
 521		/// delete by pk(entity)
 522		/// </summary>	
 523		public int Delete(MoDept entity,DbTransaction trans)
 524		{	try
 525            {
 526				DbCommand dbCommand=ConstructDeleteCommand(entity);			
 527				return db.ExecuteNonQuery(dbCommand,trans);
 528			}
 529			catch(DbException ex)
 530			{			
 531				trans.Rollback();
 532				HandleDbException(ex,entity);				
 533			}
 534			return 0;
 535		}	
 536		public int Delete(MoDept entity)
 537		{
 538			try
 539            {
 540				DbCommand dbCommand=ConstructDeleteCommand(entity);			
 541				return db.ExecuteNonQuery(dbCommand);
 542			}
 543			catch(DbException ex)
 544			{				
 545				HandleDbException(ex,entity);				
 546			}
 547			return 0;
 548		}		
 549		/// <summary>
 550		/// mul-delete
 551		/// </summary>	
 552		public int Delete(List<MoDept> entities,DbTransaction trans)
 553		{
 554			int rc=0;	
 555			int j=0;
 556			DbCommand dbCommand=ConstructDeleteCommand();		
 557			try
 558			{
 559			    for(j=0;j<entities.Count;j++)
 560				{	
 561					db.SetParameterValue(dbCommand,"Dept_Id",entities[j].Dept_Id);
 562					rc+=db.ExecuteNonQuery(dbCommand,trans);
 563				}
 564			}
 565			catch(DbException ex)
 566			{	
 567				if(trans!=null)trans.Rollback();
 568				HandleDbException(ex,entities[j]);				
 569			}			 
 570			return rc;			
 571		}		
 572		public int Delete(List<MoDept> entities)
 573		{
 574			int rc = 0;			
 575			using (DbConnection cnn = db.CreateConnection())
 576            {      
 577				DbTransaction trans=null;
 578				try
 579				{
 580					cnn.Open();
 581					using(trans = cnn.BeginTransaction())
 582					{
 583						rc=Delete(entities,trans);
 584						trans.Commit();
 585					}               
 586					cnn.Close();
 587				}
 588				catch(DbException ex)
 589				{	
 590					if(trans!=null)trans.Rollback();
 591					HandleDbException(ex);				
 592				}	
 593			}
 594			return rc;
 595		}
 596		
 597		
 598		public int Delete(DataTable dt,DbTransaction trans)
 599		{
 600			int rc=0;			
 601			int j=0;
 602			DbCommand dbCommand=ConstructDeleteCommand();		
 603			try
 604			{
 605			    for(j=0;j<dt.Rows.Count;j++)
 606				{	
 607					db.SetParameterValue(dbCommand,"Dept_Id",dt.Rows[j]["Dept_Id"]);
 608					rc+=db.ExecuteNonQuery(dbCommand,trans);
 609				}
 610			}
 611			catch(DbException ex)
 612			{	
 613				if(trans!=null)trans.Rollback();
 614				HandleDbException(ex,dt.Rows[j]);				
 615			}			 
 616			return rc;			
 617		 	
 618		}		
 619		public int Delete(DataTable dt)
 620		{
 621			int rc=0;
 622		    using (DbConnection cnn = db.CreateConnection())
 623            {
 624				DbTransaction trans=null;
 625				try
 626				{
 627					cnn.Open();				
 628					using(trans = cnn.BeginTransaction())
 629					{
 630						rc = Delete(dt, trans);
 631						trans.Commit();
 632					}
 633					cnn.Close();
 634                }
 635                catch(DbException ex)
 636				{	
 637					if(trans!=null)trans.Rollback();
 638					HandleDbException(ex);				
 639				}
 640                
 641            }
 642            return rc;
 643		}		
 644		/// <summary>
 645		/// ??????????
 646		/// </summary>	
 647		public int Delete(string Dept_Id,DbTransaction trans)
 648		{			 
 649			try
 650			{
 651				DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
 652				 
 653				db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,Dept_Id);
 654				 
 655				return db.ExecuteNonQuery(dbCommand,trans);
 656			}
 657			catch(DbException ex)
 658			{	
 659				if(trans!=null)trans.Rollback();
 660				HandleDbException(ex);				
 661			}	
 662			return 0;
 663		}	
 664		
 665		public int Delete(string Dept_Id)
 666		{
 667			try
 668			{
 669				DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
 670				 
 671				db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,Dept_Id);
 672				 
 673				return db.ExecuteNonQuery(dbCommand);
 674			}
 675			catch(DbException ex)
 676			{			 
 677				HandleDbException(ex);				
 678			}	
 679			return -1;
 680		}
 681		#endregion
 682		
 683		#region Query All by condition
 684		/// <summary>
 685		/// ????WHERE????????,?condition????????
 686		/// </summary>		
 687		public DataSet Query(string condition)
 688		{			
 689			try
 690			{
 691				return db.ExecuteDataSet(CommandType.Text,String.IsNullOrEmpty(condition)?SQL_SELECT:SQL_SELECT+condition);	 
 692			}
 693			catch(DbException ex)
 694			{			 
 695				HandleDbException(ex);				
 696			}	
 697            return new DataSet();
 698		}		
 699	    public DataSet Query(string condition, DbTransaction trans)
 700        {   
 701            try
 702            {
 703                return db.ExecuteDataSet(trans, CommandType.Text, String.IsNullOrEmpty(condition) ? SQL_SELECT : SQL_SELECT + condition);
 704            }
 705            catch(DbException ex)
 706			{			 
 707				HandleDbException(ex);				
 708			}	
 709            return new DataSet();
 710        }
 711		#endregion
 712		
 713		#region GetEntity(s)
 714		/// <summary>
 715		///????????????
 716		/// </summary>
 717	    public MoDept GetEntity(string mDept_Id )
 718		{			 
 719			MoDept entity = new MoDept();
 720			entity.Dept_Id=mDept_Id;
 721			if(GetEntityEx(entity))
 722			{
 723				return entity;
 724			}
 725			else
 726			{
 727				return null;
 728			}
 729		}
 730		
 731		public MoDept GetEntity(string mDept_Id ,DbTransaction trans)
 732		{			 
 733			MoDept entity = new MoDept();
 734			entity.Dept_Id=mDept_Id;
 735			if(GetEntityEx(entity,trans))
 736			{
 737				return entity;
 738			}
 739			else
 740			{
 741				return null;
 742			}
 743		}
 744		
 745		private DbCommand ConstructSelectOneCommand(Database db,MoDept entity)
 746		{
 747			DbCommand dbCommand=db.GetSqlStringCommand(SQL_SELECT_ONE);
 748			 
 749			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,entity.Dept_Id);
 750			return dbCommand;
 751		}
 752		private DbCommand ConstructSelectOneCommand(Database db)
 753		{
 754			DbCommand dbCommand=db.GetSqlStringCommand(SQL_SELECT_ONE);
 755			 
 756			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString);
 757			return dbCommand;
 758		}
 759		private void PopulateEntityByDataReader(IDataReader reader,ref MoDept entity)
 760		{
 761			if (!reader.IsDBNull(0)) entity.Dept_Id = reader.GetString(0);
 762			if (!reader.IsDBNull(1)) entity.Parent_Dept_Id = reader.GetString(1);
 763			if (!reader.IsDBNull(2)) entity.Dept_Name = reader.GetString(2);
 764			if (!reader.IsDBNull(3)) entity.Dept_Tel = reader.GetString(3);
 765			if (!reader.IsDBNull(4)) entity.Dept_Manager_Code = reader.GetString(4);
 766			if (!reader.IsDBNull(5)) entity.Rem = reader.GetString(5);
 767		}
 768		
 769		/// <summary>
 770		///????????????????????????????????.
 771		///??????????????????????ref????
 772		/// </summary>
 773		public bool GetEntityEx(MoDept entity)
 774		{
 775			DbCommand dbCommand=ConstructSelectOneCommand(db,entity);
 776			try
 777			{
 778				using(IDataReader reader=db.ExecuteReader(dbCommand))
 779				{   
 780					if(reader.Read()) 
 781					{
 782						PopulateEntityByDataReader(reader,ref entity);
 783						return true;
 784					}
 785				}
 786			}
 787			catch(DbException ex)
 788			{	 
 789				HandleDbException(ex,entity);				
 790			}	
 791			return false;
 792		}
 793		public bool GetEntityEx(MoDept entity,DbTransaction trans)
 794		{		
 795			DbCommand dbCommand=ConstructSelectOneCommand(db,entity);
 796			try
 797			{
 798				using(IDataReader reader=db.ExecuteReader(dbCommand,trans))
 799				{   
 800					if(reader.Read())
 801					{
 802						PopulateEntityByDataReader(reader,ref entity);	
 803						return true;
 804					}
 805				}
 806			} 
 807			catch(DbException ex)
 808			{			 
 809				trans.Rollback();
 810				HandleDbException(ex,entity);				
 811			}	
 812			return false;
 813			
 814		}
 815		
 816		public List<MoDept> GetEntities(string condition,DbTransaction trans)
 817		{
 818			List<MoDept> list=new List<MoDept>();			
 819			try
 820			{				
 821			    using(IDataReader reader= db.ExecuteReader(trans,CommandType.Text, String.IsNullOrEmpty(condition) ? SQL_SELECT : SQL_SELECT+ condition))
 822			    {
 823				    while(reader.Read())
 824					{			
 825						MoDept entity=new MoDept();
 826						PopulateEntityByDataReader(reader,ref entity);	
 827						list.Add(entity);
 828					}
 829			    }
 830			}
 831			catch(DbException ex)
 832			{			 
 833				trans.Rollback();
 834				HandleDbException(ex);				
 835			}	
 836			return list;
 837			
 838		}
 839		
 840		
 841		public List<MoDept> GetEntities(string condition)
 842		{
 843			List<MoDept> list=new List<MoDept>();	
 844			try
 845			{
 846				using(IDataReader reader= db.ExecuteReader(CommandType.Text, String.IsNullOrEmpty(condition) ? SQL_SELECT : SQL_SELECT+" " + condition))
 847				{   
 848					while(reader.Read())
 849					{	
 850						MoDept entity=new MoDept();
 851						PopulateEntityByDataReader(reader,ref entity);	
 852						list.Add(entity);
 853					}
 854				}
 855			}
 856			catch(DbException ex)
 857			{	 
 858				HandleDbException(ex);				
 859			}	
 860			return list;
 861		}
 862		#endregion
 863		
 864		#region Count
 865		/// <summary>
 866		/// ????????
 867		/// </summary>
 868		public int GetCount()
 869		{
 870			return Convert.ToInt32(db.ExecuteScalar(CommandType.Text,
 871			"SELECT COUNT(*) FROM Dept "));
 872		}
 873		
 874		public int GetCount(DbTransaction trans)
 875		{
 876			try
 877			{
 878			    return Convert.ToInt32(db.ExecuteScalar(trans,CommandType.Text,
 879			    "SELECT COUNT(*) FROM Dept "));
 880		    }
 881		    catch(DbException ex)
 882			{	 
 883				HandleDbException(ex);				
 884			}	
 885			return -1;
 886		}
 887		
 888		/// <summary>
 889		/// ????WHERE?????
 890		/// </summary>
 891		public int GetCount(string condition)
 892		{
 893			return Convert.ToInt32(db.ExecuteScalar(CommandType.Text,String.IsNullOrEmpty(condition)?
 894			"SELECT COUNT(*) FROM Dept ":"SELECT COUNT(*) FROM Dept "+condition ));
 895		}
 896		
 897		public int GetCount(string condition,DbTransaction trans)
 898		{
 899			try
 900			{
 901			   return Convert.ToInt32(db.ExecuteScalar(trans,CommandType.Text,String.IsNullOrEmpty(condition)?
 902			   "SELECT COUNT(*) FROM Dept ":"SELECT COUNT(*) FROM Dept "+condition ));
 903		    }
 904			catch(DbException ex)
 905			{	 
 906				HandleDbException(ex);				
 907			}	
 908			return -1;
 909		}
 910		#endregion
 911		
 912		#region Check isExist
 913		/// <summary>
 914		/// ????????????
 915		/// </summary>
 916        public bool IsExist(string mDept_Id )
 917		{
 918			string sql="SELECT COUNT(*) FROM Dept  WHERE Dept_ID=@Dept_Id  ";
 919			
 920			DbCommand dbCommand=db.GetSqlStringCommand(sql);
 921			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,mDept_Id);
 922			return Convert.ToInt32(db.ExecuteScalar(dbCommand))>0;
 923		}
 924		
 925		public bool IsExist(string mDept_Id ,DbTransaction trans)
 926		{			 
 927			string sql="select count(*) from Dept  where Dept_ID=@Dept_Id  ";
 928			
 929			DbCommand dbCommand=db.GetSqlStringCommand(sql);
 930			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,mDept_Id);
 931			try
 932			{
 933			   return Convert.ToInt32(db.ExecuteScalar(dbCommand,trans))>0;
 934			}
 935			catch
 936			{
 937			    trans.Rollback();
 938				throw;
 939			}		 
 940		}
 941		
 942		public bool IsExist(MoDept entity,DbTransaction trans)
 943		{			
 944			string sql="select count(*) from Dept  where Dept_ID=@Dept_Id  ";
 945			
 946			DbCommand dbCommand=db.GetSqlStringCommand(sql);
 947			#region set pk where parameters
 948			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,entity.Dept_Id);
 949			#endregion
 950			try
 951			{
 952			   return Convert.ToInt32(db.ExecuteScalar(dbCommand,trans))>0;
 953			}
 954			catch
 955			{
 956			    trans.Rollback();
 957				throw;
 958			}		 
 959		}
 960		
 961		public bool IsExist(MoDept entity)
 962		{
 963			string sql="select count(*) from Dept  where Dept_ID=@Dept_Id  ";
 964			
 965			DbCommand dbCommand=db.GetSqlStringCommand(sql);
 966			#region set pk where parameters
 967			db.AddInParameter(dbCommand,"Dept_Id",DbType.AnsiString,entity.Dept_Id);
 968			#endregion
 969			 
 970			return Convert.ToInt32(db.ExecuteScalar(dbCommand))>0;
 971			 	 
 972		}
 973		#endregion
 974		
 975		#region Handle Exception
 976		private void HandleDbException(DbException ex)
 977		{
 978			//TODO:handle db exception.
 979			//Please throw application custom exception.
 980			
 981			throw ex;
 982		}
 983		private void HandleDbException(DbException ex, MoDept entity)
 984		{			
 985			//NOTE:If you use SQL Server/Oracle/Db2... ,Please add the [internal static class ErrorCodes]
 986			//Use the ErrorCodes like below:
 987			//if (ex.Number == ErrorCodes.SqlUserRaisedError)
 988            //{
 989            //    switch (ex.State)
 990            //    {
 991            //    case ErrorCodes.ValidationError:
 992            //        string[] messageParts = ex.Errors[0].Message.Split(':');
 993            //        throw new RepositoryValidationException(
 994            //            mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
 995            //            messageParts[1], ex);
 996
 997            //    case ErrorCodes.ConcurrencyViolationError:
 998            //        throw new ConcurrencyViolationException(ex.Message, ex);
 999
1000            //    }
1001            //}
1002			
1003            //TODO:Please contruct your customer Exception.
1004            //throw new RepositoryFailureException(ex);      
1005			throw ex;           
1006		}
1007		private void HandleDbException(DbException ex,DataRow entity)
1008		{
1009			//TODO:If you use SQL Server/Oracle/Db2... ,Please add the [internal static class ErrorCodes]
1010			//if (ex.Number == ErrorCodes.SqlUserRaisedError)
1011            //{
1012            //    switch (ex.State)
1013            //    {
1014            //    case ErrorCodes.ValidationError:
1015            //        string[] messageParts = ex.Errors[0].Message.Split(':');
1016            //        throw new RepositoryValidationException(
1017            //            mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
1018            //            messageParts[1], ex);
1019
1020            //    case ErrorCodes.ConcurrencyViolationError:
1021            //        throw new ConcurrencyViolationException(ex.Message, ex);
1022
1023            //    }
1024            //}
1025			
1026            //TODO:Please contruct your customer Exception.
1027            //throw new RepositoryFailureException(ex);      
1028			throw ex;           
1029		}
1030		#endregion
1031	}
1032}	
1033#endregion
1034