To display data for a database table through a LINQ to SQL class

Source: Internet
Author: User

1. Database operations: First create a simple table in the database SPJ

2. Create a new ASP. NET Blank site

3. Create a new Web Form named Showdata.aspx

Add two controls to the source interface: a button control and a GridView control

<div>            <asp:button id= "btnshow" runat= "Server" text= "click Show GridView Table" onclick= "Btnshow_click"/>            <asp:gridview id= "GridView" runat= "Server" >            </asp:GridView>        </div>

You can see the design interface

4. Create a new LINQ to SQL class named: DATACLASSES.DBML, make a database connection in Server Explorer, connect to the database in 1, and drag the table SPJ to the design interface

5. Create a new class named: DALShowData.cs

Code:

Using system;using system.collections;using system.collections.generic;using system.linq;using System.Web;///< summary>///Dalshowdata Summary description///</summary>namespace dal{public    class Dalshowdata    {public        IEnumerable daldataquary ()        {            dataclassesdatacontext db=new dataclassesdatacontext ();            IEnumerable A = from B in db. SPJ                Select New                {                    B.sno,                    b.pno,                    b.jno,                    b.qty                };            return A;}}    

6. Open the background code Showdata.aspx.cs, write the button click event code

Using system;using system.collections;using system.collections.generic;using system.linq;using System.Web;using System.web.ui;using system.web.ui.webcontrols;using dal;public partial class showdata:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {    }    protected void Btnshow_click (object sender, EventArgs e)    {        dalshowdata dalshowdata=new dalshowdata ();        var data = Dalshowdata.daldataquary ();        This. Gridview.datasource = data;        This. Gridview.databind ();    }}

7. View the showdata.aspx in the browser and view the results

Click the button to see the data for the SPJ table:

Http://www.cnblogs.com/wiming/p/3470300.html

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.