如何在文本框中显示sql数据

时间:2015-12-13 15:08:38

标签: c#

我的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();

2 个答案:

答案 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();
}