19. DataTable 클래스
.NET프로그래밍/ADO.NET 2009. 9. 28. 14:40 |
DataTable : 메모리상의 테이블, 즉, DB의 Table개체와 일대일로 매핑할 수 있는 그릇(클래스)
- Rows와 Columns를 가짐
-------------------------------------------------------------------------------------
==> [FrmDataTable.aspx] 소스 및 디자인
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmDataTable.aspx.cs" Inherits="FrmDataTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<p>
</p>
<form id="form1" runat="server">
<div>
카테고리<asp:GridView ID="ctlCategoryList" runat="server">
</asp:GridView>
<br />
<br />
상품<asp:GridView ID="ctlProductList" runat="server">
</asp:GridView>
<br />
</div>
</form>
</body>
</html>
-------------------------------------------------------------------------------------
==> [FrmDataTable.aspx.cs]
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class 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] 데이터셋 : Database
DataSet ds = new DataSet(); // 한개 이상의 테이블을 담을 수 있는 그릇
//[5] Fill()
da.Fill(ds, "Market");
//[6] 데이터테이블 : Table
DataTable dt1 = ds.Tables[0]; // Categories
DataTable dt2 = ds.Tables[1]; // Products
//[7] GridView에 바인딩
this.ctlCategoryList.DataSource = dt1; ctlCategoryList.DataBind();
this.ctlCategoryList.DataSource = dt2; ctlProductList.DataBind();
//[8] 마무리
con.Close();
}
}
// DataTable 클래스 - 메모리에 있는 데이터로 구성된 하나의 테이블을 나타냅니다.
-------------------------------------------------------------------------------------
[실행결과]
--> "DataTable"클래스와 "GridView" 2개를 사용하여 웹페이지에서 [SQL Server]내의 [Market]데이터베이스 안에 있는 [dbo.Categories]테이블과 [dbo.Products]테이블을 출력하였다.
'.NET프로그래밍 > ADO.NET' 카테고리의 다른 글
21. DataRow 클래스 (0) | 2009.09.28 |
---|---|
20. DataView 클래스 (0) | 2009.09.28 |
18. DataSet 클래스 (0) | 2009.09.28 |
17. SqlTransaction 클래스 / TransactionScope 클래스 (0) | 2009.09.28 |
16. 수정(Modify) / 삭제(Delete) (0) | 2009.09.28 |