/// <summary> ///Data Layer/// </summary> Public Partial classGasbottles:igasbottles {#regionIgasbottles Members PublicModel.gasbottles Getmodel (intGasbottlesid) { vardb =Dbcontext.lgscmsdatacontext; Try { varGS = db. Gasbottles.firstordefault (s = = S.id = =gasbottlesid); if(GS! =NULL) {model.gasbottles gasbottlesinfo= Gs. Converttoentity<model.gasbottles>(); returnGasbottlesinfo; } } Catch(Exception ex) {Throwex; } return NULL; } Public BOOLAdd (Model.gasbottles gasbottles) {BOOLFlag =false; Try { vardb =Dbcontext.lgscmsdatacontext; Datalinqentity.gasbottles GS= gasbottles.converttoentity<datalinqentity.gasbottles>(); Db. Gasbottles.insertonsubmit (GS); Db. SubmitChanges (); Flag=true; } Catch(Exception ex) {flag=false; Throwex; } returnFlag; } Public BOOLUpdate (Model.gasbottles gasbottles) {BOOLFlag =false; varChangeddata = gasbottles.converttoentity<datalinqentity.gasbottles>(); vardb =Dbcontext.lgscmsdatacontext; Try { varUpdatetarget = db. Gasbottles.singleordefault (i = i.id = =gasbottles.id); //copyproperties (ref updatetarget, changeddata);Changeddata.gettype (). GetProperties (). Where (P= P.getcustomattributes (typeof(Columnattribute),false). Any ()). ToList (). ForEach (P= P.setvalue (Updatetarget, P.getvalue (Changeddata,NULL),NULL)); Db. SubmitChanges (); Flag=true; } Catch(Exception ex) {flag=false; Throwex; } returnFlag; } Public BOOLDelete (intGasbottlesid) { BOOLFlag =false; vardb =Dbcontext.lgscmsdatacontext; Try { varGS = db. Gasbottles.firstordefault (s = = S.id = =gasbottlesid); if(GS! =NULL) {Gs.deleteflag=1; Db. SubmitChanges (); } } Catch(Exception ex) {flag=false; Throwex; } returnFlag; } /// <summary> ///New or updated/// </summary> /// <param name= "Gasbottles" ></param> /// <param name= "Gasbottlesid" ></param> /// <returns></returns> Public BOOLSave (Model.gasbottles gasbottles, out intGasbottlesid) { BOOLFlag =false; varChangeddata = gasbottles.converttoentity<datalinqentity.gasbottles>(); vardb =Dbcontext.lgscmsdatacontext; Try { //= + new varUpdatetarget = db. Gasbottles.singleordefault (i = i.id = =gasbottles.id); if(Updatetarget = =NULL) {db. Gasbottles.insertonsubmit (Changeddata); Db. SubmitChanges (); Gasbottlesid=changedData.ID.ToInt (); Flag=true; } //= Modify Else { //copyproperties (ref updatetarget, changeddata);Changeddata.gettype (). GetProperties (). Where (P= P.getcustomattributes (typeof(Columnattribute),false). Any ()). ToList (). ForEach (P= P.setvalue (Updatetarget, P.getvalue (Changeddata,NULL),NULL)); Db. SubmitChanges (); Gasbottlesid=updateTarget.ID.ToInt (); Flag=true; } } Catch(Exception ex) {flag=false; Throwex; } returnFlag; } #endregion }View Code
Private void Copyproperties<t> (ref t Target, T Source) { foreach in Target.gettype (). GetProperties ()) { if (PI). CanWrite && PI. CanRead) { nullnull) ; }}} #endregion
View Code
DbContext
Public classDbContext {/// <summary> /// /// </summary> Private ReadOnly Static stringConnectionString =sqlhelper.sqlconnstring; #region[=>winfrom Way]//private static Wlmqgasbottlesdatacontext _wlmqgasbottlesdatacontext; ///// <summary> ///// ///// </summary> //Public static Wlmqgasbottlesdatacontext Wlmqgasbottlesdatacontext//{ //Get// { //return _wlmqgasbottlesdatacontext new Wlmqgasbottlesdatacontext (connectionString); // } //} #endregion #region[=>web Way] Public Staticwlmqgasbottlesdatacontext Wlmqgasbottlesdatacontext {Get{Wlmqgasbottlesdatacontext Context= httpcontext.current.items["Wlmqgasbottlesdatacontext"] asWlmqgasbottlesdatacontext; if(Context = =NULL) {Context=NewWlmqgasbottlesdatacontext (connectionString); httpcontext.current.items["Wlmqgasbottlesdatacontext"] =context; } returncontext; } } Public Staticlgscmsdatacontext Lgscmsdatacontext {Get{Lgscmsdatacontext Context= httpcontext.current.items["Lgscmsdatacontext"] asLgscmsdatacontext; if(Context = =NULL) {Context=NewLgscmsdatacontext (connectionString); httpcontext.current.items["Lgscmsdatacontext"] =context; } returncontext; } } #endregion }View Code
Using CRUD operations in a LINQ to SQL three-tier architecture