ASP.NET中利用存储过程向DropDownList中绑定数据

来源:互联网 发布:网络定制礼物 编辑:程序博客网 时间:2024/06/18 11:58

ASP.NET中利用存储过程向DropDownList中绑定数据

在前台界面中设定一DropDownList --ID为DropDownList1

       

string id= Session["ID"].ToString();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["数据库 名 称"].ConnectionString.ToString());
conn.Open();


        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "SelectName";
        cmd.Parameters.Add("@id", id);

 

        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;
        sda.Fill(ds, "数据库名称");

        MediaDL.DataSource = ds.Tables["数据库名称"].DefaultView;
        MediaDL.DataTextField = "所要绑定字段名(例如名字)";
        MediaDL.DataBind();
        conn.Close();

 

 

原创粉丝点击