One, LINQ to SQL class
High-integrated database access technology
LINQ can be used instead of the previous ADO. Eliminates the massive work of the entity classes and data access classes that knock the code yourself.
Entity class:
Add a LINQ to SQL class----Select the database to use----drag into the table you want to use as if the entity class was finished.
If a property extension is made, the class is added under App_Code, and the class name matches the table name. Add the class to the access modifier with partial to make the class part of the entity class
Data Access classes:
No more data access classes are built under App_Code.
Write directly in the database context where the data access class is used.
Conn. Users.tolist (); is to convert the users table to a generic collection. It is equivalent to querying the entire method.
Add Data:
Each piece of data is an entity class object. To instantiate it first.
Assigns a value to each property of the object.
Adding data operations using the database context
Added method: Con. Users.insertonsumbit (object);
Perform the method to be manipulated: con. SubmitChanges ();
Delete data:
This data should be checked before deleting the data.
Querying through IDs This data returns an object
Users u = con. Users.where (r=>r.ids.tostring () ==ids). FirstOrDefault ();
Where parentheses are followed by the lambda expression R for each row of data,=> is the basic format FirstOrDefault () returns the first data or the return null
If u is not empty, the database context is used to delete it.
Con. Users.deleteonsubmit (object);
Con. SubmitChanges ();
Changes to the data:
The modification of the data also requires that the data be checked first and re-assigned the property to be modified for the returned object.
Then commit the change directly.
Con. SubmitChanges ();
Lambda expression
Con. Users.where (r=>r.ids.tostring () ==ids). FirstOrDefault () Return the first data that satisfies the condition or return an empty
If it is a two-condition query, use &&
Con. Users.where (r=>r.username== "Zhangsan" && r.password== "123"). FirstOrDefault ();
"2017-06-01" LINQ base +LAMBDA expression implements additions and deletions to the database