LINQ Learning (9): simple application of LINQ to SQL

Source: Internet
Author: User

The previous chapter briefly introduced the basic knowledge of LINQ. How should we use LINQ to connect to the database and operate the data? The following is an example:

 

1. Create an empty website.

 

2. Create the linqdata. MDF database, add the table product, and then add data to the table.

 

3. Create linqproduct. dbml and drag the table product to it.

 

4. Create a web form, add a gridview control to the page, and write the bindingCode.

Default. aspx:

 <  Body  >      <  Form  ID = "Form1"  Runat  = "Server"  >          <  ASP: gridview  ID  = "Productgridview"  Runat  = "Server"  > </  ASP: gridview  >      </  Form  >  </ Body  > 

Default. aspx. CS:

     Protected   Void Page_load ( Object  Sender, eventargs e ){  If (! Ispostback) {productlist ();}}  Protected   Void  Productlist () {linqproductdatacontext lp = New  Linqproductdatacontext (); VaR Query = From P In  LP. Product  Select  P; productgridview. datasource = Query; productgridview. databind ();} 

 

5. Run the displayed result.

 

6. Simple Description.

(1) The linqproductdatacontext class inherits from system. Data. LINQ. datacontext. The datacontext class represents the main entry point of the LINQ to SQL framework and provides a series of database operation methods.

(2) convert the poruduct table in the database to the seal class poruduct, and convert the fields in the table to the corresponding fields of the class, you can perform operations through the object method.

(3) according to my understanding, the LINQ to SQL framework will compile the corresponding query method into an SQL statement and then obtain data from the database.

 

Author: forevernome
Source: http://www.cnblogs.com/ForEvErNoME/
You are welcome to repost or share it, but be sure to declare it Article Source. If the article is helpful to you, I hope you can Recommendation Or Follow

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.