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)