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