C# 从 SQL DataTable 填充组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12900062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:53:16  来源:igfitidea点击:

C# Fill combo box from SQL DataTable

c#sql

提问by Pseudorandom

DataTable _dt = new DataTable();

using (SqlConnection _cs = new SqlConnection("Data Source=COMNAME; Initial Catalog=DATABASE; Integrated Security=True"))
{
    string _query = "SELECT * FROM Doctor";
    SqlCommand _cmd = new SqlCommand(_query, _cs);

    using (SqlDataAdapter _da = new SqlDataAdapter(_cmd))
    {
        _da.Fill(_dt);
    }
}
cbDoctor.DataSource = _dt;
foreach(DataRow _dr in _dt.Rows)
{
    cbDoctor.Items.Add(_dr["name"].ToString());
}

There was an Error...

有一个错误...

The result is System.Data.DataRowViewinstead of data from database..

结果是System.Data.DataRowView而不是来自数据库的数据..

采纳答案by Lukasz M

I'm not yet sure what is the exact error in your code, but if you're ok with not using DataTable, you can do it this way:

我还不确定您的代码中的确切错误是什么,但是如果您可以不使用DataTable,您可以这样做:

using (SqlConnection sqlConnection = new SqlConnection("connstring"))
{
    SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Doctor", sqlConnection);
    sqlConnection.Open();
    SqlDataReader sqlReader = sqlCmd.ExecuteReader();

    while (sqlReader.Read())
    {
        cbDoctor.Items.Add(sqlReader["name"].ToString());
    }

    sqlReader.Close();
}

For more information take a look at SqlDataReader reference on MSDN.

有关更多信息,请查看MSDN上的SqlDataReader 参考

In orer to find the issue in the original code you posted, please provide information in which line you get the exception (or is it an error that prevents application from compiling?) and what is its whole message.

为了在您发布的原始代码中找到问题,请提供您在哪一行收到异常的信息(或者它是否是阻止应用程序编译的错误?)以及它的完整消息是什么。

回答by NDraskovic

I think the problem is that you are trying to insert multiple columns into a comboBox (which can accept only one column). Adjust your SELECT statement so that it combines all of the data you need into one column:

我认为问题在于您试图将多列插入到组合框(只能接受一列)中。调整您的 SELECT 语句,使其将您需要的所有数据合并到一列中:

Example: ′SELECT Name + ' ' LastName + ' '+ ID AS 'DoctorData' FROM Doctor′

示例:'SELECT Name + ' ' LastName + ' '+ ID AS 'DoctorData' FROM Doctor'

By using the + operator you can combine multiple columns from the database, and represent it as a single piece of data. I think that your code should work after that (you can even comment the foreach loop, I think that adding data source to your comboBox will be enough)

通过使用 + 运算符,您可以组合数据库中的多个列,并将其表示为单个数据。我认为您的代码应该在那之后工作(您甚至可以评论 foreach 循环,我认为将数据源添加到您的组合框就足够了)

回答by crypted

You could also specify DisplayMember property of combobox to any of the column name. For example if you want to display Name field, you could do

您还可以将组合框的 DisplayMember 属性指定为任何列名称。例如,如果您想显示名称字段,您可以这样做

Combobox1.DisplayMember="Name";