One, LINQ to SQL class
High-integrated database access technology
LINQ can be used instead of the previous ADO, eliminating the massive work of the entity classes and data access classes that you knock code
Entity class:
Add a LINQ to SQL class----Select the database to use----drag into the table you want to use to save immediately is equivalent to creating an entity class
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's the equivalent of querying the whole method.
Ii. using LINQ to perform related operations on databases
1. Add Data
Each piece of data is an entity class object. Instantiate it first, and then assign a value to each property of the object.
To add data operations in a data access class
Added method: Con. Users.insertonsumbit (object);
Commit changes to the database after the execution of the method: Con. SubmitChanges ();
2. 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 a lambda expression, R represents a Users object,=> is the basic format, FirstOrDefault () returns the first data or the return of an empty
If u is not empty, it is deleted in the data access class.
Con. Users.deleteonsubmit (object);
Con. SubmitChanges ();
3. Data modification
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.
Last con. SubmitChanges ();
4. Simple data query
Directly in the data access class according to the object's properties to query, multiple query conditions in the middle with && connection;
such as con. Users.where (r=>r.username== "Zhangsan" && r.password== "123"). FirstOrDefault ();
LINQ base +LAMBDA expression to database additions and deletions and simple queries