In the database, the current field includes the operator, the operation time, the SQL statement, the Modified field, the field original value, the operator's identity.
/// <summary> ///add an action log/// </summary> /// <param name= "strSQL" >The SQL statement executed</param> /// <param name= "username" >Executive Person</param> /// <param name= "Dentity" >identity of the person who performed</param> /// <param name= "cmdparms" >Parameters</param> Public Static voidAddlog (StringBuilder strSQL,stringUsernamestringDentity,paramssqlparameter[] cmdparms) {Graduate.Model.OperateLog Model=NewGraduate.Model.OperateLog (); Model.dentity=Dentity; Model.operatuser=username; Model.operattime=DateTime.Now.ToString (); stringContent = strSQL. ToString (). Substring (0, strSQL. ToString (). IndexOf ("Values")); StringBuilder Sblog=NewStringBuilder (); Sblog. Append (Content+"Values"); Sblog. Append (" ( "); foreach(SqlParameter spinchcmdparms) {Sblog. Append ("'"+ sp. Value +"',"); } sblog. ToString (). TrimEnd (','); Sblog. Insert (Sblog. Length-1,')'); Model.operatcontent= Sblog. ToString (). TrimEnd (','); Model. Prevvalue=""; Model. NewValue=""; Graduate.DAL.OperateLog DAL=NewOperatelog (); Dal. ADD (model); } /// <summary> ///Delete operation Log/// </summary> /// <param name= "strSQL" >SQL statements</param> /// <param name= "username" >Executive Person</param> /// <param name= "Dentity" >identity of the person who performed</param> /// <param name= "id" >key Fields</param> /// <param name= "hash" >parameters (hasttable)</param> Public Static voidDeletelog (StringBuilder strSQL,stringUsernamestringDentity,stringid,hashtable Hash) {Graduate.Model.OperateLog Logmodel=NewGraduate.Model.OperateLog (); Logmodel.dentity=Dentity; Logmodel.operatuser=username; StringBuilder Sblog=NewStringBuilder (); stringConten = strSQL. ToString (). Substring (0, strSQL. ToString (). IndexOf ('=')); Sblog. Append (Conten+" = "); Sblog. Append (ID. ToString ()); Logmodel.operatcontent=Sblog. ToString (); Logmodel.operattime=DateTime.Now.ToString (); stringPrevval =""; foreach(DictionaryEntry deinchhash) {Prevval+ = de. Key +"="+ de. Value +","; } prevval= Prevval. TrimEnd (','); Logmodel. Prevvalue=Prevval; Logmodel. NewValue=""; Graduate.DAL.OperateLog Logdal=NewOperatelog (); Logdal. ADD (Logmodel); } /// <summary> ///Update operation Log/// </summary> /// <param name= "Obj1" >Original Object</param> /// <param name= "Parmhash" >Update field set (Hashtable)</param> /// <param name= "username" >Executive Person</param> /// <param name= "Dentity" >identity of the person who performed</param> /// <param name= "TableName" >Table name</param> /// <param name= "keytable" >Storage conditions and values (Hashtable)</param> Public Static voidUpdatelog (ObjectObj1,hashtable Parmhash,stringUsernamestringDentity,stringtablename,hashtable keytable) {propertyinfo[] Properties= Obj1. GetType (). GetProperties (BindingFlags.Public |bindingflags.instance); StringBuilder CONSB=NewStringBuilder (); Hashtable Prevhash=NewHashtable (); Hashtable Newhash=NewHashtable (); Consb. Append ("Update"+ TableName +"Set"); foreach(PropertyInfo Piinchproperties) { stringPiname =Pi. Name; stringPivalue = convert.tostring (pi. GetValue (Obj1,NULL)); foreach(DictionaryEntry deinchParmhash) { if(Piname = =de. Key.tostring ()) {if(Pivalue! =de. Value.tostring ()) {if(!keytable. Contains (DE. Key) {Consb. Append (DE. Key+"= '"+ de. Value +"'"+","); } prevhash. ADD (Piname, Pivalue); Newhash. Add (Piname, DE. Value); }}}} CONSB. Remove (CONSB. Length-1,1); Consb. Append ("where"); foreach(DictionaryEntry deinchkeytable) {CONSB. Append (DE. Key+"= '"+ de. Value +"'"+" and"); } CONSB. Remove (CONSB. Length-4,3);//remove the "and" stringPrevval =""; foreach(DictionaryEntry deinchPrevhash) {Prevval+ = de. Key +"="+ de. Value +","; } prevval= Prevval. TrimEnd (','); stringnewval =""; foreach(DictionaryEntry deinchNewhash) {newval+ = de. Key +"="+ de. Value +","; } newval= newval. TrimEnd (','); Graduate.Model.OperateLog Logmodel=NewGraduate.Model.OperateLog (); Logmodel.dentity=Dentity; Logmodel. NewValue=newval; Logmodel.operatcontent=CONSB. ToString (); Logmodel.operattime=DateTime.Now.ToString (); Logmodel.operatuser=username; Logmodel. Prevvalue=Prevval; Graduate.DAL.OperateLog Logdal=NewOperatelog (); Logdal. ADD (Logmodel); }
Because the SqlHelper class is used, the general SQL statement is similar to insert into A (field) values (@field), and if the arguments are passed in, the SQL statement does not change because the StringBuilder connection string is used. And I need to be specific to the database to save the SQL can also execute the point, so need to do a variety of splicing conversion work.
There's a place where the results are right, and I don't understand why. See "model.operatcontent = Sblog for add operation." ToString (). TrimEnd (', '); " ;
After splicing the INSERT statement, always followed by a "," do not know how to produce. Need to TrimEnd remove "," is correct, do not understand
Another need to say is that the reason is placed in the DAL layer, because the "executor" and "Executor identity" of the two fields, then you need to add this method for the various classes of the modified deletion method, the workload is relatively large, is very bad, mainly because do not want to modify SqlHelper. In fact, you can use reflection to find out the properties and values of each object, and then compare, the method can be more abstract, this later
. NET operations for each table detailed to the log of changes recorded in the field