Simple tutorial on LINQ

Source: Internet
Author: User

It is short for language intergrated query, that is, "integrated language query". using LINQ, you can use database tables as classes, which is simple and convenient.

Next we will give a brief introduction to the usage of LINQ in a short space.

Preparation: Create a table in the SQL Server database

This is a preparation work. You can also use access, Oracle, and other databases. Here, SQL Server is used because it has the best fit with Microsoft's. NET platform.

This article uses the previously created exercise database.

Create a C # project in Visual Studio (any)

Both the console, winform, and ASP support LINQ. Here we use the ConsoleProgramFor example.

Create a console program project first.

Click View in the menu bar and select server resource manager to open the server resource manager window.

 

 

Right-click the "Data Connection" option and select "add connection" in the pop-up menu to open the "Select data source" dialog box.

 

In the displayed dialog box, select "Microsoft SQL Server" and click "continue ".

 

In the next dialog box, enter the server name (localhost for the local machine), select "Windows Account Logon" or "SQL Server user logon", and then select the database to connect. Click OK ".

 

After readingIn the "server resource manager" window on the right, the database we added is here.

 

In Solution Explorer, right-click the project name and choose add> Add from the shortcut menu. In the displayed dialog box, select "LINQ to SQL class ", the file name must comply with naming rules.

 

The added page is as follows:

 

At this time, you can add all the tables in the database you have added. All the elements after adding them will appear here, including primary keys and Foreign keys.

 

Open the dataclasses1.designer. CS file to see if all the tables have become C # classes.

 

Now that we have the LINQ class, we can use it like other classes.

Return to the program. CS file and enter the following example in the main method:Code:

 Using  System;  Using  System. Collections. Generic;  Using System. LINQ;  Using  System. text;  Namespace  Linqtest {  Class  Program {  Static   Void Main ( String  [] ARGs ){  //  Create a dataclass object, which is declared in dataclass1.dbml. Dataclasses1datacontext da = New Dataclasses1datacontext ();  Try  {  //  Here, the LINQ query statements are slightly different from those in SQL. You can search for them online.                  VaR Query = From S In  Da. sale_item  Where S. unit_price> 2000                              Select  S; console. writeline (); console. writeline ( "  Query Result  "  );  //  The query statement returns a set. To view the content, you must use foreach to loop.                  Foreach (Sale_item s In  Query) {console. writeline (S. order_no + "   " + S. prod_no + "   " + S. qty + "  " + S. unit_price );}  //  The above are the query statements and results.  //  Insert data to the database Customer Cus = New  Customer (); cust_no = "  C1111  "  ; Cust_name = "  Abcdefg "  ; Cus. tel_no = "  12345678  "  ; Cus.zip = "  100000  "  ; Cuz. ADDR = "  Abcdefg  "  ;  // These two statements are a bit like transactions that have been learned before. They are written to the database only after they are added.  Da. Customer. insertonsubmit (CUs); da. submitchanges (); console. writeline (); console. writeline (  "  Execution result after data is inserted  "  );  VaR Query2 = From C In Da. Customer Select  C;  Foreach (Customer CIn  Query2) {console. writeline (C. cust_no + "   " + C. cust_name + "   " + C. ADDR + "   " + C. tel_no + "   " + C.zip );}  //  Modify database data                  VaR Result =From C In Da. Customer Where C. cust_no = "  C1111  "   Select  C;  Foreach (Customer C In  Result) {C. cust_name = "  Gfedcba  " ; C. tel_no = "  87654321  "  ; Cus.zip = "  999999  "  ; Cuz. ADDR = "  Gfedcba  "  ;} Da. submitchanges (); console. writeline (); console. writeline ( "  Execution result after data modification  "  ); Query2 = From C In Da. Customer Select  C;  Foreach (Customer C In  Query2) {console. writeline (C. cust_no + "   " + C. cust_name +"   " + C. ADDR + "   " + C. tel_no + "   " + C.zip );}  //  Delete data                  VaR Result2 = From C In  Da. Customer  Where C. cust_no ="  C1111  "                                Select  C;  Foreach (Customer C In  Result2) {da. Customer. deleteonsubmit (c);} da. submitchanges (); console. writeline (); console. writeline (  "  Execution result after data deletion  "  ); Query2 = From C In Da. Customer Select  C;  Foreach (Customer C In  Query2) {console. writeline (C. cust_no + "   " + C. cust_name + "   " + C. ADDR + "   " + C. tel_no + "  " + C.zip );}}  Catch  (System. Exception ex) {console. writeline (ex. tostring ());}}}} 

The execution result is as follows:

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.