186 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			186 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C#
		
	
	
	
|  | using System; | |||
|  | using System.Collections.Generic; | |||
|  | using System.Text; | |||
|  | <EFBFBD><EFBFBD> | |||
|  | using System.Data.SqlClient; | |||
|  | using  System.Data; | |||
|  | using System.Data.Common; | |||
|  | using myLlog; | |||
|  | using Tracen.Utils; | |||
|  | using Tracen.Data.DataAccess; | |||
|  | using System.Data.OleDb; | |||
|  |   | |||
|  | namespace Tracen.Data.BaseObject | |||
|  | { | |||
|  |     public class BaseModel_Imp   | |||
|  |     { | |||
|  |         protected String _strSql = "";        | |||
|  |         protected int _op_flag; | |||
|  |         protected int _cust_op_flag; | |||
|  |         protected BaseModel ModelObj; | |||
|  |         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(); | |||
|  |         } | |||
|  | 
 | |||
|  |         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(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); | |||
|  |         } | |||
|  |         | |||
|  | <EFBFBD><EFBFBD> | |||
|  |         protected virtual void Assem_Model(SqlDataReader reader, BaseModel obj) | |||
|  |         { | |||
|  |             return; | |||
|  |         } | |||
|  | 
 | |||
|  |      <EFBFBD><EFBFBD> | |||
|  |         protected virtual void CmdPrepare(SqlCommand sqlCmd) | |||
|  |         { | |||
|  |             //sqlCmd.CommandText = "insert into a values(@a)"; | |||
|  |             // sqlCmd.Parameters.Add("@a","a"); | |||
|  |             return; | |||
|  |         } | |||
|  |          | |||
|  | 
 | |||
|  |     <EFBFBD><EFBFBD> | |||
|  |         private int ExecuteNoneQuery() | |||
|  |         { | |||
|  |             using (Execution Exe = new Execution()) | |||
|  |             { | |||
|  |                 CmdPrepare(Exe.SqlCmd); | |||
|  |                | |||
|  |                 if (Exe.SqlCmd.CommandText.Length == 0) return 0;  | |||
|  |                 int affectrows = Exe.Execute(); | |||
|  |                 if (_op_flag == (int)op_flag.add) //ȡ<><C8A1>pk | |||
|  |                 { | |||
|  |                     ModelObj.custOperFlag = 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD>ѯ<EFBFBD><D1AF>־ | |||
|  |                     ModelObj._id = getPk(); | |||
|  |   //              getModel(ModelObj); | |||
|  |                 }  | |||
|  |                  | |||
|  |                   _cust_op_flag = 0;  | |||
|  |                   _op_flag = 0; | |||
|  |                     return affectrows; | |||
|  |                  | |||
|  |                    | |||
|  |             } | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         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() | |||
|  |         { | |||
|  |             _op_flag = (int)op_flag.getPk; | |||
|  |             string tmp = ExecuteDsQuery().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; | |||
|  |         } | |||
|  |     } | |||
|  | } |