본문 바로가기
ASP.NET

LINQ 써서 간단하게 데이터 가져와서 출력하기

by BeGeek 2016. 12. 2.

먼저 데이터를 가져오기 위해 솔루션 탐색기에서 솔루션 선택후 새 항목 추가(ctrl+shift+a) > 데이터 > 'LINQ to SQL 클래스'클릭(.dbml 페이지가 생성 됨)

서버 탐색기에서 데이터 연결에서 DB선택 후 가져올 테이블을 선택하고 드래그하여 .dbml페이지에 올린다.

 

디자인탭에서 GridView하나 넣고

WebForm3.aspx.cs에다 LINQ구문 넣어주고 실행하면 끝

}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication5
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var persons = from s in db.GetTable<Person>()
                          where s.FirstName.Contains("Ken")
                          select s;

            this.GridView1.DataSource = persons;
            this.GridView1.DataBind();
        }

      
    }

순식간에 데이터출력!

 

댓글