如何从动态文本框中的MS数据库中获取所有数据[ROW]



我有这样的代码

int cLeft=0;
public System.Windows.Forms.TextBox AddNewTextBox()
{
    System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    string query = " select * FROM DotMatrix;
    command.CommandText = query;
    OleDbDataReader reader = command.ExecuteReader();
    this.Controls.Add(txt);
    txt.Top = (cLeft*25) + 124;
    txt.Left = 50;
    txt.Height = 20;
    txt.Width = 259;
    while (reader.Read())
    {
        txt.Text=reader["Pertanyaan"].ToString();
    }
    if (txt.Text=="")
    {
        MessageBox.Show("Pertanyaan  Habis , Akan Redirect Ke Hasil");
    }
    cLeft = cLeft + 1;
    return txt;
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
   AddNewTextBox();
}

我的问题是,为什么文本框只显示数据库中的1行???我想在pertanyaan行中显示数据[行]感谢您的答案

此行循环遍历每一行,并不断覆盖文本框值:

while (reader.Read())
{
    txt.Text=reader["Pertanyaan"].ToString();
}

因此,一遍又一遍地分配了相同的文本框。

您的文本框创建代码希望将其移至循环内部,类似的内容:

while (reader.Read())
{
    System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
    txt.Top = (cLeft*25) + 124;
    txt.Left = 50;
    txt.Height = 20;
    txt.Width = 259;
    txt.Text=reader["Pertanyaan"].ToString();
    if (txt.Text=="")
    {
        MessageBox.Show("Pertanyaan  Habis , Akan Redirect Ke Hasil");
    }
    this.Controls.Add(txt);
}

最新更新