简体   繁体   English

从数据库获取数据到ComboBox

[英]Get Data from database to ComboBox

Short one. 短一个。

A database named ComDet , and column cID(PK) , cName , cDet , mainCate(FK) , Subcat(FK). 一个名为ComDet的数据库,其列为cID(PK),cName,cDet,mainCate(FK),Subcat(FK)。

this suppose to get the data from table ComDet to the combobox.. 这假设是将数据从表ComDet获取到组合框。

DataSet ds2;

private void searchBtn_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
    conn.Open();

    SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
    ds2 = new DataSet();
    daSearch.Fill(ds2, "daSearch");
    ListU.ValueMember = "cName";
    ListU.DataSource = ds2.Tables["ComDet"];
    ListU.DropDownStyle = ComboBoxStyle.DropDownList;
    ListU.Enabled = true;
}

but it didnt work.. where did i go wrong? 但是它没有用..我在哪里出错了? The Data(cName) from database table ComDet is not shown in the combobox. 组合框中未显示数据库表ComDet中的Data(cName)。 -

Problem : You are assigning database table name ComDet as DataSource but not DataTable Name daSearch to ComboBox . 问题:您正在将数据库表名称ComDet分配为DataSource而不是DataTable名称daSearchComboBox

Solution : you need to assign valid DataTable Name to ComboBox as Datasource . 解决方案:您需要为ComboBox分配有效的DataTable名称作为Datasource

Replace This : 替换为:

ListU.DataSource = ds2.Tables["ComDet"];

With This: 有了这个:

ListU.DataSource = ds2.Tables["daSearch"];  

(or) (要么)

ListU.DataSource = ds2.Tables[0];

Complete Code: 完整的代码:

DataSet ds2;

private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();

SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["daSearch"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}

So the question asks how to get data from database to combobox . 所以这个问题问如何从数据库中获取数据到combobox Personally, I find using the DataSet class to be unpreferable - it is quite prone to errors like the asker is experiencing here. 就个人而言,我发现使用DataSet类是不可取的-它很容易出现错误,例如asker在此遇到的错误。

Try this approach. 试试这种方法。 Which reads all the cName into a List and binds the List to the ComboBox . 读取所有的cNameList并绑定ListComboBox Simple and readable code. 简单易读的代码。 The use of using statements also ensures that the unmanaged resources are released efficiently. using语句的using还可以确保有效释放非托管资源。

private void searchBtn_Click(object sender, EventArgs e)
{
    var list = new List<string>();
    using (var conn = new SqlConnection())
    {
        conn.ConnectionString = 
            "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
        conn.Open();
        using (var cmd = new SqlCommand("SELECT cName FROM ComDet", conn))
        {
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    list.Add(Convert.ToString(reader["cName"]));
                }
            }
        }
   }
   ListU.DataSource = new BindingSource(list, null);
   ListU.DropDownStyle = ComboBoxStyle.DropDownList;
   ListU.Enabled = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM