我正在尝试根据在comobox中选择的值更新richtext框的内容。它正在从SQL中提取PartnerName和Response,如下所示;
目前只能获取其中一个合作伙伴名称及其响应。我错过了什么?
string connectionString =
VendorFinder.Properties.Settings.Default.VendorFinderConnectionString;
SqlDataReader dr;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT PartnerName, Response FROM
dbo.ResponseMessage", con);
con.Open();
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
if (dr.Read())
{
comboBox3.Items.Add(dr.GetValue(0));
richTextBox1.Text = dr["Response"].ToString();
dr.Close();
}
else
{
MessageBox.Show("No Record Found. Please try again", "Vendor
Finder", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
由于
答案 0 :(得分:0)
您只调用dr.Read()一次,以便获取我在循环中放入的所有数据
当您调用它时,Read()方法充当光标移动到数据中的下一个位置 ,所以你必须继续调用它,你得到所有的数据
string connectionString =
VendorFinder.Properties.Settings.Default.VendorFinderConnectionString;
SqlDataReader dr;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT PartnerName, Response FROM
dbo.ResponseMessage", con);
con.Open();
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
if(dr.HasRows)
{
while (dr.Read())
{
comboBox3.Items.Add(dr.GetValue(0));
richTextBox1.Text = dr["Response"].ToString();
}
dr.Close();
}
else
{
MessageBox.Show("No Record Found. Please try again", "Vendor
Finder", MessageBoxButtons.OK, MessageBoxIcon.Information);
}