FrmDataTableSelect.aspx |
<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> |
FrmDataTableSelect.aspx.cs |
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 Num, 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("",
"Num Desc"); // RowFilter속성과 Sort속성의 기능 합침
// 반복하면서 출력
for (int i = 0; i < arr.Length; i++)
{
this.lstCategoryList.Items.Add( new ListItem( arr[i]["CategoryName"].ToString(), arr[i]["Num"].ToString()));
} } |
결과화면 |
[그림21-1] |
'ASP.NET' 카테고리의 다른 글
-- DataRelation (Xml 변환) (0) | 2009.09.30 |
---|---|
22.ADO.NET - MakeDataSet (0) | 2009.09.29 |
20.ADO.NET - SqlParamter (0) | 2009.09.29 |
-- 주소록 DB사용 (0) | 2009.09.29 |
19.ADO.NET - RowFilter (0) | 2009.09.28 |
Comments