ASP.NET

18.ADO.NET - DataRow

Godffs 2009. 9. 28. 15:39
반응형
DataTable의 데이터 행
DataRow 개체와 DataColumn 개체는 DataTable의 기본 구성 요소
DataRow 개체 및 해당 속성과 메서드를 사용하여 DataTable의 값을 검색, 계산, 삽입, 삭제 및 업데이트

FrmDataRow.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

        카테고리 리스트 출력<br />

        <br />

        <asp:DropDownList ID="lstCategoryList" runat="server">

        </asp:DropDownList>

   

    </div>

    </form>

</body>

</html>



FrmDataRow.aspx.cs

protected void Page_Load(object sender, EventArgs e)

{

   SqlConnection con = new SqlConnection(

       ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

   con.Open();

   SqlCommand cmd = new SqlCommand("Select * From Categories", con);

   cmd.CommandType = CommandType.Text;

   SqlDataAdapter da = new SqlDataAdapter(cmd);

   DataSet ds = new DataSet();

   da.Fill(ds, "Categories");

 

  //직접바인딩

   //[!] 드롭다운리스트에 바인딩

   this.lstCategoryList.DataSource = ds;

   this.lstCategoryList.DataTextField = "categoryname";

   this.lstCategoryList.DataTextField = "num";

   this.lstCategoryList.DataBind(); 


   con.Close();

}


결과화면

[그림18-1]


이름을 카테고리에 출력하는 예제입니다.
FrmDataRow.aspx.cs

protected void Page_Load(object sender, EventArgs e)

{

   SqlConnection con = new SqlConnection(

       ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

   con.Open();

   SqlCommand cmd = new SqlCommand("Select * From Categories", con);

   cmd.CommandType = CommandType.Text;

   SqlDataAdapter da = new SqlDataAdapter(cmd);

   DataSet ds = new DataSet();

   da.Fill(ds, "Categories");


   //List<T> 같은 방법으로 출력

   for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

   {

       DataRow dr = ds.Tables[0].Rows[i]; // 한개 레코드를 DataRow 담기

       // Text : 카테고리명, Value : 카테고리ID 출력

       lstCategoryList.Items.Add(

           new ListItem(dr["CategoryName"].ToString(), dr["Num"].ToString()));    

   }

   con.Close();

}


결과확인

[그림18-2]



반응형