学习笔记--ado.net 数据绑定

 

 protected void Page_Load(object sender, EventArgs e)

{SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=teachers;Integrated Security=True;");    

SqlDataAdapter da = new SqlDataAdapter("select*from tea_info where s_sex='boy'", conn);            

DataSet ds = new DataSet();           

 da.Fill(ds, "result");           

 this.DropDownList1.DataSource = ds.Tables["result"];   

this.DropDownList1.DataTextField = "s_name";          

   this.DropDownList1.DataBind();}

两种方法的不同,细细体会。

 /*  SqlConnection conn = new SqlConnection("sever=.;database=teachers;integrated security=true");      

       conn.Open();     

        SqlCommand cmd = new SqlCommand("select*form tea_info",conn);          

   SqlDataReader dr = cmd.ExecuteReader();            

while (dr.Read())       

      {                

 this.DropDownList1.Items.Add(dr["s_name"].ToString());

     }          

   conn.Close();                     */

 

posted @ 2012-03-19 17:55  zhangmengjie  Views(100)  Comments(0)    收藏  举报