ORM (O-object object, r-relation relationship, m-mapping mapping)
Table name-Class name
Column name--property name
Table relationships-member objects of a class
linq--Integrated Query Language sql--Structured Query language
LINQ includes: LINQ to SQL, LINQ to Object, LINQ to DataSet, LINQ to Entity
LinQ to SQL
Step one: Build a LINQ to SQL Class (project right → add → class → data, LINQ to SQL class → add a database connection in Server Explorer → find the table you want, drag to the page)
Step Two: Instantiate the context object.
Step Three: Operation
1. Increase:
//Instantiation ContextMydbdatacontext context =NewMydbdatacontext (); //1. Making ObjectsInfo data =NewInfo (); Data. Code="p211"; Data. Name="Zhou Qing"; Data. Nation="N001"; Data. Sex=false; Data. Birthday=NewDateTime (1990,1,1); //2. Register the newly created object in the contextcontext.Info.InsertOnSubmit (data); //3. Submit the data to the databaseContext. SubmitChanges ();
2. By deleting:
Mydbdatacontext context =NewMydbdatacontext (); //1, in the Info Table query Code "p003" Data varQ = context.Info.Where (p = = P.code = ="p003");//var q is a dynamic deduction type that automatically infers the data type. (p = = P.code = = "p003") meaning: The data in P is passed in (p. Code = = "P003") to Judge if(Q.count () >0) {Info data=Q.first (); //2. Registrationcontext. Work.deleteallonsubmit (data. work); Context. Family.deleteallonsubmit (data. Family); Context.Info.DeleteOnSubmit (data); //3. Submit to Databasecontext. SubmitChanges (); }
3. Change:
Static voidMain (string[] args) {Mydbdatacontext Context=NewMydbdatacontext (); //1, in the Info table to find the code "P001" data, put in memory varQ = context.Info.Where (p = = P.code = ="P001"); if(Q.count () >0) {Info data=Q.first (); //2. Modify the data in memoryData. Name ="Mr. Hu"; Data. Nation="n001"; Data. Sex=false; //3. Submit the modified data to the databasecontext. SubmitChanges (); } }
4, check: 4.1, check all
Static voidMain (string[] args) {Mydbdatacontext Context=NewMydbdatacontext (); //1. Query all data in info table varQ =context. Info; //2. Display foreach(Info datainchq) {//data. Nation1: The national object that the current person corresponds to. Console.WriteLine (data. name+"\ t"+data. Nation1.name); //data. Work: A collection of working records for the current person foreach(Work workinchdata. Work) {Console.WriteLine ("\ t"+work. firm+"\ t"+Work . Depart); } } }
4.2. Search by condition
var " p211 "); // the default is to return the collection if 0) // See if the data is found in the collection { = Q.first (); // take the first object out of the Console.WriteLine (data. Nation1.name + data. Name); }
Mydbdatacontext context =NewMydbdatacontext (); //Query All varQ = fromPinchContext. InfoSelectp; varQ =context. Info; //single-condition query varQ = fromPinchContext. InfowhereP.code = ="p003" Selectp; varQ = context.Info.Where (p = = P.code = ="p003"); //Multi-Criteria Query varQ = fromPinchContext. CarwhereP.price > -&& P.brand = ="b002" Selectp; varQ = Context. Car.where (p = p.price > -&& P.brand = ="b002");//effect is the same as the next line varQ = Context. Car.where (p = p.price > -). Where (p = = P.brand = ="b002");//lambda expression (the most streamlined function) varQ = fromPinchContext. CarwhereP.price > -|| P.brand = ="b002" Selectp; varQ = Context. Car.where (p = p.price > -|| P.brand = ="b002"); //Fuzzy Query varQ = fromPinchContext. CarwhereP.name.contains ("5")SelectP//include varQ = fromPinchContext. CarwhereP.name.startswith ("Audi")SelectP//Start varQ = fromPinchContext. CarwhereP.name.endswith ("Audi")SelectP//End varQ = Context. Car.where (p = p.name.contains ("5"));//include varQ = Context. Car.where (p = p.name.startswith ("Audi"));//Start varQ = Context. Car.where (p = p.name.endswith ("type"));//End varQ = fromPinchContext. CarwhereP.name.substring (2,1) =="5" SelectP//a third character varQ = Context. Car.where (p = p.name.substring (2,1) =="5");//The third character is a 5//DISTINCT Query varQ = ( fromPinchContext. CarSelectP.brand). Distinct ();//Go heavy varQ = Context. Car.select (p = p.brand). Distinct ();//Go heavy//Connection Query--the association between objects is the point. varQ = Context. Car.where (p = p.brand1.productor.prod_name = ="FAW Toyota"); //page Out varQuery = context. Car.skip (2*4). Take (4); //Sort varQuery = context. Car.orderby (p = p.price);//Ascending varQuery = context. Car.orderbydescending (p = p.price);//Descending//Collection Operations varQuery1 = context. Car.where (p = p.brand1.productor.prod_name = ="FAW Toyota"); varQuery2 = context. Car.where (p = p.price > -); //intersection varquery =Query1. Intersect (Query2); //and set varquery =Query1. Union (Query2); //Difference Set varquery =Query1. Except (Query2); //complement set varQuery = context. Car.except (Query1);
// Linq2sql New Mydbdatacontext (); List<Car> list = context. Car.tolist (); // turn into a collection // linq2object--to In-memory collection operations var query = list. (p = p.price); // Sort by Price
LinQ to SQL