如何从C#中将记录从数据库提取到下拉列表 [英] How to fetch record from database to dropdown in C#

查看:71
本文介绍了如何从C#中将记录从数据库提取到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中获取记录并将其显示在下拉列表中。



我尝试了什么:



ddldropdown.SelectedIndex = ddldropdown.Items.IndexOf(ddldropdown.Items.FindByText(sdr [value]。ToString()));



还有除上述方法以外的任何其他方法从数据库获取记录到下拉列表。

i want to fetch record from database and show it into dropdown.

What I have tried:

ddldropdown.SelectedIndex = ddldropdown.Items.IndexOf(ddldropdown.Items.FindByText(sdr["value"].ToString()));

is there any other method other then above method to fetch record from database to dropdown.

推荐答案

它可用于您的问题的样本



Its sample you can use for your issue

SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[dbConnection].ConnectionString;

SqlCommand comd = new SqlCommand();
comd.CommandText = "select distinct FIELD from TABLE";
comd.Connection = conn;
conn.Open();
SqlDataAdapter dap = new SqlDataAdapter(comd);
dap.Fill(ds2);
try
{
    for (int a = 0; a < ds2.Tables[0].Rows.Count; a++)
    {
        ddldropdown.Items.Add(ds2.Tables[0].Rows[a][0].ToString().ToUpper());
    }
}
catch (Exception ex)
{
    MessageBox.Show("" + ex.Message);
}
finally
{
    conn.Close();
}


这篇关于如何从C#中将记录从数据库提取到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆