Orm
O-object Object
R-relation relationship
M-mapping Mapping
O--M--R
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
First step: Establish Linq2sql class
Step Two: Instantiate the context object.
Step Three: Operation
First, Increase:
1. Build the object.
Info data = new info ();
Data. Code = "p211";
Data. Name = "Zhou Qing";
Data. Sex = false;
Data. Nation = "N001";
Data. Birthday = new DateTime (1990, 1, 2);
2. Register the newly created object in the context.
Context.Info.InsertOnSubmit (data);
3. Submit
Context. Submitchange ();
Second, delete:
Mydbdatacontext context = new Mydbdatacontext ();
1. Find
var q = context.Info.Where (p = = P.code = = "P003");
if (Q.count () > 0)
{
Info data = Q.first ();
2. Registration
Context. Work.deleteallonsubmit (data. work);
Context. Family.deleteallonsubmit (data. Family);
Context.Info.DeleteOnSubmit (data);
3. Submit
Context. SubmitChanges ();
}
Third, change:
1. Find
2. Change
3. Submit
Four, check:
Query all
Mydbdatacontext context = new Mydbdatacontext ();
All personnel
var q = context. Info;
Show
foreach (Info data in Q)
{
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 in data. Work)
{
Console.WriteLine ("\ T" +work. firm+ "\ t" +work. Depart);
}
}
Query based on PRIMARY key
var q = from P in context. Info where P.code = = "p211" select P;
var q = context.Info.Where (p = = P.code = = "p211"). Where (p = = P.nation1.name = = "Han"); Lambda expression (the most streamlined function)
var q = context.Info.Where (p = = P.code = = "p211" && p.nation1.name== "Han");
All of the above queries return a collection by default.
Querying primary Key objects:
var q = context.Info.Where (p = = P.code = = "p211"); The default is to return the collection
if (Q.count () > 0)//See if the data is found in the collection.
{
Info data = Q.first (); Take the first object out of the
Console.WriteLine (data. Nation1.name + data. Name);
}
ORM Operation 20141128