284 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			284 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C#
		
	
	
	
|  | using System; | |||
|  | using System.Data.SqlClient; | |||
|  | using  System.Data; | |||
|  | using DeiNiu.Utils; | |||
|  | //using DeiNiu.Data.DataAccess; | |||
|  | 
 | |||
|  | namespace DeiNiu | |||
|  | { | |||
|  |     public class BaseModel_Imp  { | |||
|  |      protected String _strSql = "";        | |||
|  |         protected int _op_flag; | |||
|  |         protected int _cust_op_flag; | |||
|  |         protected BaseModel modelObj; | |||
|  |         protected int _rownumStart; | |||
|  |         protected int _rownumEnd; | |||
|  |         protected int _totalCount; | |||
|  |         protected internal  int Add(BaseModel obj) | |||
|  |         { | |||
|  |             modelObj = obj; | |||
|  |             _op_flag = (int)op_flag.add; | |||
|  |            return ExecuteNoneQuery(); | |||
|  | 
 | |||
|  |         } | |||
|  |         protected internal   int Update(BaseModel obj) | |||
|  |         { | |||
|  |             modelObj = obj; | |||
|  |             _op_flag = (int)op_flag.update; | |||
|  |             return ExecuteNoneQuery(); | |||
|  |         } | |||
|  |         protected internal   int Delete(BaseModel obj) | |||
|  |         { | |||
|  |             modelObj = obj; | |||
|  |             _op_flag = (int)op_flag.delete; | |||
|  |             return ExecuteNoneQuery(); | |||
|  |         } | |||
|  |         protected internal  DataSet Query(BaseModel exampleObj) | |||
|  |         { | |||
|  |               | |||
|  |             modelObj = exampleObj; | |||
|  |             _op_flag = (int)op_flag.queryExample; | |||
|  |             return ExecuteDsQuery(); | |||
|  |               | |||
|  |              | |||
|  |         } | |||
|  |         protected internal DataSet QueryActived() | |||
|  |         { | |||
|  |             _op_flag = (int)op_flag.queryActived; | |||
|  |             return ExecuteDsQuery(); | |||
|  |         } | |||
|  |         public DataSet QueryCount() | |||
|  |         { | |||
|  |             _op_flag = (int)op_flag.getCount; | |||
|  |             return ExecuteDsQuery(); | |||
|  |               | |||
|  |         } | |||
|  |         protected internal DataSet Query() | |||
|  |         { | |||
|  |             | |||
|  |             _op_flag = (int)op_flag.queryAll; | |||
|  |             return ExecuteDsQuery(); | |||
|  | 
 | |||
|  |         } | |||
|  |         protected internal DataSet CustQuery(String strSql) | |||
|  |         { | |||
|  |             _strSql = strSql; | |||
|  |             return new Execution(_strSql).getData(); | |||
|  |         } | |||
|  |         protected internal DataSet CustQuery(String strSql,SqlConnection conn) | |||
|  |         { | |||
|  |             _strSql = strSql; | |||
|  |             return new Execution(_strSql,conn).getData(); | |||
|  |         } | |||
|  | 
 | |||
|  |         protected internal DataSet CustQuery(BaseModel exampleObj) | |||
|  |         { | |||
|  |             modelObj = exampleObj; | |||
|  |             _cust_op_flag = exampleObj.custOperFlag; | |||
|  |             return ExecuteDsQuery(); | |||
|  |         } | |||
|  | 
 | |||
|  |         protected internal int CustOper(BaseModel exampleObj) | |||
|  |         { | |||
|  |             modelObj = exampleObj; | |||
|  |             _cust_op_flag = exampleObj.custOperFlag; | |||
|  |             return ExecuteNoneQuery(); | |||
|  |         } | |||
|  | 
 | |||
|  |         //it must not be overrided        | |||
|  |         internal  void getModel(BaseModel obj) | |||
|  |         { | |||
|  |             modelObj = obj; | |||
|  |             if (obj.custOperFlag != 0) //  customzation method | |||
|  |             { | |||
|  |                 _cust_op_flag = obj.custOperFlag; | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  |                 _op_flag = (int)op_flag.getObj;   //to get the object by key word | |||
|  |             } | |||
|  | 
 | |||
|  |             Assem_Model(ExecuteRdQuery(),obj); | |||
|  |         } | |||
|  |         | |||
|  | 
 | |||
|  |         //to set object's attribute from ds. | |||
|  |         //it must be overrided   | |||
|  |         protected virtual void Assem_Model(SqlDataReader reader, BaseModel obj) | |||
|  |         { | |||
|  |             return; | |||
|  |         } | |||
|  | 
 | |||
|  |         /* | |||
|  |         protected virtual void CmdPrepare( OleDbCommand SqlCmd) | |||
|  |         { | |||
|  |             //SqlCmd.CommandText = "insert into a values(@a)"; | |||
|  |             // SqlCmd.Parameters.Add("@a","a"); | |||
|  |             return; | |||
|  |         } | |||
|  |          */ | |||
|  |         protected virtual void CmdPrepare(SqlCommand sqlCmd) | |||
|  |         { | |||
|  |             //sqlCmd.CommandText = "insert into a values(@a)"; | |||
|  |             // sqlCmd.Parameters.Add("@a","a"); | |||
|  |             return; | |||
|  |         } | |||
|  |          | |||
|  | 
 | |||
|  |        /* | |||
|  |         private int ExecuteNoneQuery() | |||
|  |         { | |||
|  |             using (oledbExecution Exe = new oledbExecution()) | |||
|  |             { | |||
|  |                 CmdPrepare(Exe.OleCmd); | |||
|  |                 this._cust_op_flag = 0; | |||
|  |                 this._op_flag = 0; | |||
|  |                 return Exe.Execute(); | |||
|  |             } | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         private DataSet ExecuteDsQuery() | |||
|  |         { | |||
|  |             using (oledbExecution Exe = new oledbExecution()) | |||
|  |             { | |||
|  |                 CmdPrepare(Exe.OleCmd); | |||
|  |                 this._cust_op_flag = 0; | |||
|  |                 this._op_flag = 0; | |||
|  |                 return Exe.getData(); | |||
|  |             } | |||
|  |         } | |||
|  |         protected int getSequence(String SequenceName) | |||
|  |         { | |||
|  | 
 | |||
|  |             using (oledbExecution Exe = new oledbExecution()) | |||
|  |             { | |||
|  |                 return Exe.getNextSeqValue(SequenceName);  | |||
|  |             } | |||
|  |         } | |||
|  |        | |||
|  | 
 | |||
|  |       * */ | |||
|  |         private int ExecuteNoneQuery() | |||
|  |         { | |||
|  |             using (Execution Exe = new Execution()) | |||
|  |             { | |||
|  |                 CmdPrepare(Exe.SqlCmd); | |||
|  |                | |||
|  |                 if (Exe.SqlCmd.CommandText.Length == 0) return 0;  | |||
|  |                 int returnValue =0; | |||
|  |                 if (_op_flag == (int)op_flag.add)  //insert, to get pk. | |||
|  |                 { | |||
|  |                     modelObj.custOperFlag = 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD>ѯ<EFBFBD><D1AF>־                      | |||
|  |                   | |||
|  |                     modelObj._id = Exe.Execute(true); | |||
|  |                     returnValue = modelObj._id; | |||
|  |                    /* | |||
|  |                     Exe.Execute(); | |||
|  |                     modelObj._id=getPk(); | |||
|  |                     returnValue = modelObj._id; | |||
|  |                     */ | |||
|  |   //              getModel(modelObj); | |||
|  |                 }  | |||
|  |                 else   //update | |||
|  |                 { | |||
|  |                  returnValue = Exe.Execute();  | |||
|  |                 } | |||
|  |                   _cust_op_flag = 0;  | |||
|  |                   _op_flag = 0; | |||
|  |                  | |||
|  |                  return  returnValue; | |||
|  |                  | |||
|  |                    | |||
|  |             } | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         private SqlDataReader ExecuteRdQuery() | |||
|  |         { | |||
|  |             using (Execution Exe = new Execution()) | |||
|  |             { | |||
|  |                 CmdPrepare(Exe.SqlCmd); | |||
|  |                 _cust_op_flag = 0; | |||
|  |                 _op_flag = 0; | |||
|  |                 return Exe.GetDataReader(); | |||
|  |             } | |||
|  |         } | |||
|  |         private DataSet ExecuteDsQuery() | |||
|  |         { | |||
|  |             using (Execution Exe = new Execution()) | |||
|  |             { | |||
|  |                 CmdPrepare(Exe.SqlCmd); | |||
|  |                 this._cust_op_flag = 0; | |||
|  |                 this._op_flag = 0; | |||
|  |                 if (Exe.SqlCmd.CommandText.Length == 0) return null; | |||
|  |                 return Exe.getData(); | |||
|  |             } | |||
|  |         } | |||
|  |         protected int getSequence(String SequenceName) | |||
|  |         { | |||
|  | 
 | |||
|  |             using (Execution Exe = new Execution()) | |||
|  |             { | |||
|  |                 return Exe.getNextSeqValue(SequenceName); | |||
|  |             } | |||
|  |         } | |||
|  |            | |||
|  |        // protected  int getPk(SqlConnection conn) | |||
|  |        protected  int getPk() | |||
|  |         { | |||
|  |         	 | |||
|  |             _op_flag = (int)op_flag.getPk; | |||
|  |             string tmp = ExecuteDsQuery().Tables[0].Rows[0][0].ToString(); | |||
|  |             return tmp != string.Empty | |||
|  |                        ? Convert.ToInt32(tmp) | |||
|  |                        : 0; | |||
|  |         /* | |||
|  |          | |||
|  |             string tmp = CustQuery("select Scope_Identity()",conn).Tables[0].Rows[0][0].ToString(); | |||
|  |             return tmp != string.Empty | |||
|  |                        ? Convert.ToInt32(tmp) | |||
|  |                        : 0;  | |||
|  |          */               | |||
|  |                         | |||
|  |            | |||
|  |                         | |||
|  |         } | |||
|  |              | |||
|  | 
 | |||
|  |         protected object VerifyDbnull(object obj ) | |||
|  |         { | |||
|  |             if (obj is string) | |||
|  |             { | |||
|  |                return (string) obj == string.Empty ?  DBNull.Value  : obj; | |||
|  |             } | |||
|  |             return obj; | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         protected internal int rownumStart | |||
|  |         { | |||
|  |             get | |||
|  |             { | |||
|  |                 return _rownumStart; | |||
|  |             } | |||
|  |             set | |||
|  |             { | |||
|  |                 _rownumStart = value; | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         protected internal int rownumEnd | |||
|  |         { | |||
|  |             get | |||
|  |             { | |||
|  |                 return _rownumEnd; | |||
|  |             } | |||
|  |             set | |||
|  |             { | |||
|  |                 _rownumEnd = value; | |||
|  |             } | |||
|  |         } | |||
|  |          | |||
|  |     } | |||
|  | } |