LinQ to SQL

Source: Internet
Author: User

on the Java platform , ORM this piece of Hiberante is quite . But when it comes to. NETPlatform. ORMthe frame is a little different..nhibernate,nbeatand theCastel,and what I'm going to say.LinQ to Sql.

Tosql is Microsoft's own product. It may be very rare now, but if you do a small and medium-sized project, development and deployment is good, you don't have to consider the first

three-party library. ButtoSQL is directly facing the database. I am not used to this operation, here is a brief introduction.

you want to use Linq To SQL , the implementation needs to build a good database.

Step1To build a LINQ to SQL classes file

Select Add New Item in the entity layer LinQ to SQL classes , and named.



setp2 establishing a database connection Production code

VS Open in view is Service resource management, Add Connection connection. fill in the server name with the database name. I'm going to use window here locally .

Verified the



Open the LinQ tosql file that you just created . put the data in the table on the dynamic file



after saving ORM The mapping is done, and there's no XML files, so the relative NH is simple, but there are hidden dangers. It's hard to change.

Step3 operating data

I'm working on the console here. do 3 instances, corresponding to adding a single piece of data, Deleting the collection, and querying

Add

Public   static  void Add () {//declares entity commodity commidity = new Commodity{code=guid.newguid (). ToString (), commodityname= "Mooncake", commodityprice=500,commoditytype= "2"}; Console.WriteLine ("=----begin add Commidity"), using (Dataclasses1datacontext bd= new Dataclasses1datacontext ()) {Bd. log=console.out;//Console output Bd.Commodity.InsertOnSubmit (commidity);//Call Tosql method Bd. SubmitChanges (); The////transaction mechanism is encapsulated within the SubmitChanges method Console.WriteLine ("----End add  commidity");}

There's nothing new to it.


Delete (collection )

public static void Delete () {Console.WriteLine ("-----------Begin Delete All commidity"); using (Dataclasses1datacontext BD = new Dataclasses1datacontext ()) {Bd. Log = Console.out;bd.commodity.deleteallonsubmit (Bd.Commodity.Where (p = = P.commodityname = = "Mooncake"));//delete the collection entity set for the query to, Bd. SubmitChanges ();} Console.WriteLine ("------------End Delete commidity");}

We look at the printed SQL statement is in and the form of the connected



Enquiry

look at the most important query, when it comes to queries, you have to LinQ To Object linked, the query here is the same, the same wording.

public static list<commodity> Select () {//must first instantiate the dataset Dbdataclasses1datacontext db= new Dataclasses1datacontext () ;//Collection form list<commodity> Commiditys = db. Commodity.tolist ();//Condition Form list<commodity> commditywhere=db. Commodity.where (p = = P.commodityname = = "Water"). ToList ();//Query and select var Commoditywheresel = db. Commodity.distinct (). Where (P =>p.commodityname = = "Mooncake"). Select (A = A.commodityprice);//Choose List<string> commidityname=db. Commodity.select (p = p.commodityname). ToList (); return commditywhere;}

operation and to O bject is the same, careful whether you find that only a few here is no longer a collection of data is passed, but db. The table name.

Summary

above is LinQ To SQL basic operation, only in the query to add a simple conditional restrictions, I think it is commonly used in operations, of course, there are sorting and so on. Although I do not make good use of to Sql, but also hope to the beginners of the students have some inspiration.

LinQ to SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.