블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Category

Recent Post

Recent Comment

Archive

2009. 9. 28. 14:26 .Net Project/ADO.NET 3.5
반응형
 DataTable : 메모리상의 테이블, 즉 DB의 Table개체와 일대일로 매핑할 수 있는 
                 그릇 (클래스)
        -Rows와 Columns 존재

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Category_FrmDataTable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DisplayData();            
        }
    }

    private void DisplayData()
    {
        //[1] 커넥션
        SqlConnection con = new SqlConnection
                (ConfigurationManager.ConnectionStrings
                 ["ConnectionString"].ConnectionString);
       
        con.Open();
                    
        //[2] 커멘드
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select *From Categories ;
               Select ProductID, ModelName From Products;";

        //[3] 데이터 어댑터
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd ; //명령어를 담고 있는 커맨드 개체 지정
       
        //[4] 데이터셋
        DataSet ds = new DataSet(); //한개 이상의 테이블을 담을 수 있는 그릇
       
        //[5] Fill()
        da.Fill(ds, "Market");

        //[6] [데이터테이블]
        DataTable dt1 = ds.Tables[0] ; //Categories
        DataTable dt2 = ds.Tables[1] ; //Products

        //[7] GridView 에 바인딩
        this.ctlCategoryList.DataSource = dt1; ctlCategoryList.DataBind();
        this.ctlProductList.DataSource = dt2; ctlProductList.DataBind();

        //마무리
        con.Close();
 
    }
}





반응형
posted by Magic_kit