C#-用SQL表列填充ComboBox [英] C# - Filling ComboBox with an SQL table column

查看:72
本文介绍了C#-用SQL表列填充ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,组合框被填充了,但没有填充列内的字符串,而是用 System.Data.SqlClient.SqlDataReader 填充了自身

我的代码有什么问题?

I have a little problem, the combobox is being filled, but not with the string that is inside the column, instead it fills itself with System.Data.SqlClient.SqlDataReader

What do I have wrong in my code?

try
{
    myConnection.Open();
    SqlDataReader myReader = null;
    SqlCommand myCommand = new SqlCommand("select countries from cities", myConnection);
    myReader = myCommand.ExecuteReader();
    string userText = MainMDI.globalstring;
    while (myReader.Read())
    {
        comboBox1.Items.Add(myReader);
    }
}
catch (Exception b)
{
    MessageBox.Show(b.ToString());
}



谢谢.



Thank You.

推荐答案

我猜应该是这样的:

I guess, it should be like this:

while (myReader.Read())
{
    comboBox1.Items.Add((string)myReader["Column_Name"]);
}


while (myReader.Read())
{
    comboBox1.Items.Add(myReader["Column_Name"].toString());
}


尝试一下:
Try this:
while (myReader.Read())
{
    comboBox1.Items.Add(myReader[1].ToString());
    comboBox1.DisplayMember = myReader[1].ToString();
    comboBox1.ValueMember = myReader[0].ToString();
}


这篇关于C#-用SQL表列填充ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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