数据绑定

数据绑定
分为数据绑定控件和数据源
绑定方法

 using (SqlConnection conn = new SqlConnection(connStr))//建立链接
            {
                conn.Open();//打开数据库链接
                using (SqlCommand cmd = conn.CreateCommand())//建立命令对象(通过命令对象对数据库进行操作)
                {
                    cmd.CommandText = "select id,Cname from T_City where Pid=@xxx";    //指定操作的内容
                    SqlParameter param = new SqlParameter("xxx", this.DropDownList1.SelectedValue);
                    cmd.Parameters.Add(param );
                    using(SqlDataReader reader=cmd.ExecuteReader())
                    {
                        this.DropDownList2.DataSource = reader;//获取数据绑定对象,从绑定对象中检索其数据项列表。


                        this.DropDownList2.DataTextField = "Cname";
                        this.DropDownList2.DataValueField = "id";
                        this.DropDownList2.DataBind();
                          }
                }
            }

posted on 2012-09-24 21:11  gongth_12  阅读(98)  评论(0编辑  收藏  举报

导航