Using CRUD operations in a LINQ to SQL three-tier architecture

Source: Internet
Author: User

 /// <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.