20. DataView 클래스
.NET프로그래밍/ADO.NET 2009. 9. 28. 15:21 |
- DataView : DB의 View와 같은 역할
-------------------------------------------------------------------------------------
==> [FrmDataView.aspx] 소스 및 디자인
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmDataView.aspx.cs" Inherits="FrmDataView" %>
<!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>
<form id="form1" runat="server">
<div>
상품 리스트 출력<br />
<br />
<asp:GridView ID="ctlProductList1" runat="server">
</asp:GridView>
<asp:GridView ID="ctlProductList2" runat="server">
</asp:GridView>
<asp:GridView ID="ctlProductList3" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
-------------------------------------------------------------------------------------
==> [FrmDataView.aspx.cs]
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class FrmDataView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DisplayData();
}
}
private void DisplayData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Select * From Products", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Products");
// 출력
this.ctlProductList1.DataSource = ds; //[1] 데이터셋
this.ctlProductList1.DataBind();
DataTable dt = ds.Tables["Products"]; //[2] 데이터테이블 / 문자열 인덱서 사용함
this.ctlProductList2.DataSource = dt;
this.ctlProductList2.DataBind();
DataView dv = ds.Tables[0].DefaultView; //[3] 데이터뷰 / 정수형 인덱서 사용함
this.ctlProductList3.DataSource = dv;
this.ctlProductList3.DataBind();
con.Close();
}
}
// DataView 클래스 - 정렬, 필터링, 검색, 편집 및 탐색을 위해 데이터 바인딩할 수 있는 DataTable의 사용자 지정 뷰를 나타냅니다.
[실행결과]
--> "DataView"클래스와 "GridView" 3개를 사용하여 [SQL Server] - [Market]데이터베이스 내에 있는 [dbo.Products]테이블을 3가지 방법으로 출력하였다.
'.NET프로그래밍 > ADO.NET' 카테고리의 다른 글
22. DataView..::.RowFilter 속성 (0) | 2009.09.28 |
---|---|
21. DataRow 클래스 (0) | 2009.09.28 |
19. DataTable 클래스 (0) | 2009.09.28 |
18. DataSet 클래스 (0) | 2009.09.28 |
17. SqlTransaction 클래스 / TransactionScope 클래스 (0) | 2009.09.28 |