C# 通过RadioButtonList动态绑定数据库

来源:百度知道 编辑:UC知道 时间:2024/05/14 07:27:43
我要查询某数据,首要通过RadioButtonList来动态绑定查询的数据库,随后用label显示查询结果,我该如何操作?
感谢好心网友!!!

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//你的意思是用RadioButtonList1读取数据库信息吗?然后把多少条信息显示在Label上还是什么
//你没表达清楚,我按我的意思去做啦
RadioButtonList1.DataSource = GetData();
RadioButtonList1.DataBind();
RadioButtonList1.DataTextField = "显示的值的字段名";
RadioButtonList1.DataValueField = "显示值的主键ID字段名";

}
}

private void GetData()
{
SqlConnection conn = new SqlConnection("Server=.;database=tempdb;uid=sa;pwd=123;");
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM test", conn);
sda.Fill(ds);
Label1.Text = ds.Tables[0].Rows.Count.ToString()+"条记录";
return ds.Tables[0];
//以上真是的数据库名称和表名需要自行修改相