我的SQL数据库由一个包含数字1到30的列id组成。我想在每次按下按钮时在文本框中显示数字1到30。但是我的代码只显示第一行是1.我尝试过以下代码:
SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True");
SqlCommand Comm1 = new SqlCommand("Select * from id", Conn);
Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();
if (DR1.Read())
{
textBox3.Text = DR1.GetValue(0).ToString();
}
Conn.Close();
答案 0 :(得分:1)
此行导致问题 -
textBox3.Text = DR1.GetValue(0).ToString();
这里每个循环都会覆盖textBox3
的值。
相反,您应该在每次迭代时附加textBox3
值 -
textBox3.Text = textBox3.Text + DR1.GetValue(0).ToString();
使用while
循环代替if
答案 1 :(得分:0)
你必须循环
While (DR1.read())
{
textBox3.Text += DR1.GetValue(0).ToString();
}