数据绑定

来源:互联网 发布:最小路径覆盖算法 编辑:程序博客网 时间:2024/05/16 19:44
数据绑定
分为数据绑定控件和数据源
绑定方法

 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();
                          }
                }
            }

原创粉丝点击