Webform (LINQ additions and deletions)

Source: Internet
Author: User

Linq
Highly integrated data access class that automatically maps the database structure, fully maps the table name to the class name, and fully maps the column name to a field name
Database data access can greatly reduce the amount of code.
(1) LINQ creation
To add a LINQ to SQL class, the class name needs to be the same as the database name to which you want to connect

LINQ files are dbml end, one database corresponds to one LINQ file
(2) Database connection
Open Server Explorer, click the yellow Circle button, connect to the database, pop up the Add Connection dialog box, fill in the server name (.), select what authentication to use, then the database you want to connect to, and then pull the database you want to use into the class.

(3) Data expansion

Create a new partial class (keyword: partial), such as user

  Public stringSexstr {Get        {            stringEnd ="< no >"; if( This. _sex! =NULL) {End= Convert.toboolean ( This. _sex)?"male":"female"; }            returnend; }    }     Public stringNationname {Get        {            return  This.        Nation1.nationname; }    }     Public stringBirstr {Get        {            stringEnd ="< no >"; if( This. _birthday!=NULL) {End= Convert.todatetime ( This. _birthday). ToString ("yyyy mm month DD Day"); }            returnend; }    }
User Data extension

(4) Increase and deletion check

First instantiate:

1   NULL //web-Built LINQ class name 2      Public UserData () 3     {4         new  webdatacontext (); 5     }

First, check all

1 public list<user> Select () 2 { 3 return con. User.tolist (); 4 } //user is the object name, which is the table name,

Second, add

1  Public void Insert (User u) 2 {3con. User.insertonsubmit (u); 4 con. SubmitChanges (); 5 }

Three, check a

 Public User Select (string  uname)    {        return con. User.where (r = r.username = = uname). FirstOrDefault (); // LAMDBA expression, R can be casually up    }

Iv. deletion

 Public void Delete (string  uname)    {        ///1 First isolate the object to        User u = con. User.where (r = r.username = = uname). FirstOrDefault ();         // 2 Erase        if NULL )        {            con. User.deleteonsubmit (u);            Con. SubmitChanges ();        }    }

V. Modification

 Public void Update (User u)    {        = con. User.where (r = r.username = = u.username). FirstOrDefault (); // first check and then change        if NULL )        {            = u.password;             = u.nickname;             = u.sex;             = U.birthday;             = u.nation;        }        Con. SubmitChanges ();    }

Webform (LINQ additions and deletions)

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.