블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

Category

Recent Post

Recent Comment

Archive

2009. 9. 28. 15:23 .Net Project/ADO.NET 3.5
반응형

DataRow 개체와 DataColumn 개체는 DataTable의 기본 구성 요소입니다.
DataRow 개체 및 해당 속성과 메서드를 사용하여 DataTable의 값을 검색, 계산, 삽입, 삭제 및 업데이트합니다.

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Category_FrmDataRow : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Display();
        }
    }

    private void Display()
    {
        //커넥션
        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.DataValueField = "CategoryID"; //안의 값
        this.lstCategoryList.DataBind();

        con.Close();
    }
}


- Typically populated from database
- Use DataRow
 
 //List<T>와 같은 방법으로 출력
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
             //한개의 레코드를 DataRow에 담기 
             DataRow dr = ds.Tables[0].Rows[i] ;
            
             //[1]lstCategoryList.Items.Add(dr["CategoryID"].ToString());

             //[2] 가장 일반적인 사용방법
            lstCategoryList.Items.Add(new ListItem(dr
               ["CategoryName"].ToString(),
dr["CategoryID"].ToString()));
        }






반응형
posted by Magic_kit