从不同的SQL表中获取数据并将其添加到comboBox

时间:2013-07-30 05:46:05

标签: c# sql combobox listbox

我有以下查询从两个不同的表中获取两个不同的列,并将列添加到C#中的comboBoxListBox。 这是我的代码:

SqlDataAdapter sda = new SqlDataAdapter("(select client_name from clients) UNION (select publication_name from publications)" + suffix, conn);
DataSet ds = new DataSet();
sda.Fill(ds);

我检查了dataset.Tables[0]中的5行,表 publishers 中有2行,表 clients 中有3行。

此后,我编写了此代码,将此获取的数据添加到comboBox

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
   if (ds.Tables[0].TableName.ToString() == "clients" + suffix)
     comboBox1.Items.Add(ds.Tables[0].Rows[i]["clients" + suffix].ToString());
   if (ds.Tables[0].TableName.ToString() == "publications" + suffix)
     comboBox1.Items.Add(ds.Tables[0].Rows[i]["publications" + suffix].ToString());
}

不工作!请帮忙。

1 个答案:

答案 0 :(得分:0)

有这个工作!

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                    comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
            }