从数据库中的combobox中连接两列

时间:2015-03-27 09:50:48

标签: c# sql string sqldatareader

我试图连接两列,但出了点问题.... 我的输出没有显示。

String Query = " SELECT pa_forename , pa_surname FROM [ICPS].[dbo].[parking_attendants] order by pa_forename ";
 SqlConnection conDataBase = new SqlConnection(conString);
 SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
 SqlDataReader myReader;
  try
{
  conDataBase.Open();
  myReader = cmdDataBase.ExecuteReader();
 while (myReader.Read())
                {
string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();
 comboBox1.Items.Add(pa_forename);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

2 个答案:

答案 0 :(得分:7)

替换

string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();

string pa_forename = myReader["pa_forename"] +"," + myReader["pa_surname"];

答案 1 :(得分:0)

你做错了,

string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();

您无法同时从DataReader获取两列。用这样的代码替换你的代码。上面的代码将尝试查找不存在的列。

string pa_forename = myReader["pa_forename"] +"," + myReader["pa_surname"];