这是我的代码,但它仅检索最后一条记录,而不是全部

    SqlConnection history= new SqlConnection("Data Source=.;Initial Catalog=db3;Integrated Security=True");
                history.Open();
                SqlCommand histcmd= new SqlCommand("SELECT salary FROM persontable WHERE (Name = @name)", history);
                histcmd.Parameters.AddWithValue("@name", checkname.text);

                SqlDataReader DRhistory= histcmd.ExecuteReader();
                if (DRhistory.Read())
                {
                    combobox.Text = DRhistory.GetValue(0).ToString();


                }
                history.Close();

最佳答案

如果您想获得全部,则需要一个循环:

List<string> salaryList = new List<string>();
while(DRhistory.Read())
{
    salaryList.Add(DRhistory.GetString(0));
}


要填充组合框,您可以将其添加到循环中最简单的方法中:

while(DRhistory.Read())
{
    comboBox.Items.Add(DRhistory.GetString(0));
}


或使用BindingSource您可以将列表分配为数据源:

BindingSource bs = new BindingSource();
bs.DataSource = salaryList;
comboBox.DataSource = bs;

10-08 18:55