24. DataTable..::.Select 메서드
.NET프로그래밍/ADO.NET 2009. 9. 29. 17:45 |
==> [FrmDataTableSelect.aspx] 소스 및 디자인
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmDataTableSelect.aspx.cs" Inherits="FrmDataTableSelect" %>
<!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>
<asp:ListBox ID="lstCategoryList" runat="server" Rows="5"></asp:ListBox>
</div>
</form>
</body>
</html>
--> "ListBox"를 사용했음.
-------------------------------------------------------------------------------------
==> [FrmDataTableSelect.aspx.cs]
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.UI.WebControls;
public partial class FrmDataTableSelect : 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);
SqlCommand cmd = new SqlCommand(
"Select CategoryID, CategoryName From Categories", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Categories");
DataTable dt = ds.Tables[0];
// 가전이면서, CategoryID에 내림차순으로 출력
DataRow[] arr = //dt.Select("CategoryName Like '%가전%'", "CategoryID Desc");
dt.Select("", "CategoryID Desc"); // RowFilter속성과 Sort속성의 기능 합침
// 반복하면서 출력
for (int i = 0; i < arr.Length; i++)
{
this.lstCategoryList.Items.Add(
new ListItem(
arr[i]["CategoryName"].ToString(), arr[i]["CategoryID"].ToString()));
}
}
}
// DataTable..::.Select 메서드 - 모든 DataRow 개체의 배열을 가져옵니다.
-------------------------------------------------------------------------------------
[실행결과]
--> DataTable..::.Select 메서드와 "ListBox"를 사용하여 [SQL Server] - [데이터베이스] - [Market데이터베이스]내에 있는 [dbo.Categories]테이블의 모든데이터 중 "CategoryName"을 웹페이지에서 "CategoryID"의 내림차순으로 정렬하여 (ListBox에)모두 출력하였다.
'.NET프로그래밍 > ADO.NET' 카테고리의 다른 글
(교재실습) MakeDataSet(DateSet연습) (0) | 2009.09.30 |
---|---|
(교재실습) MakeTable(DataTable / DataColumn / DataRow 연습) (0) | 2009.09.29 |
23. SqlParameter 클래스 (0) | 2009.09.29 |
파일처리 기반으로 만든 주소록 프로그램을 DB처리 기반으로 바꾸기. (0) | 2009.09.29 |
22. DataView..::.RowFilter 속성 (0) | 2009.09.28 |