PageRenderTime 68ms CodeModel.GetById 21ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/EQT_V1/EQT.DAL/DaUserRole.auto.cs

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