将数据从数据库检索到多个文本框

时间:2016-11-07 08:12:16

标签: c# c#-4.0

当我从Access数据库中检索数据到多行TextBox时,它会在同一行显示我的所有数据。在DataGridView中,一切都很好,并逐行显示。如何使我的文本框也逐行显示数据?

con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from data where [ID] like(" + textBox9.Text + ")";
cmd.Connection = con;
var reader = cmd.ExecuteReader();

while (reader.Read())
{
    textBox1.Text = reader["Target Name"].ToString();
}

2 个答案:

答案 0 :(得分:1)

textBox1.Multiline = true;

textBox1.Text += reader["Target Name"].ToString() + Environment.NewLine;

MultiLine属性设置为true,并在设置textBox1.Text时设置新行,并在末尾添加Environment.NewLine

答案 1 :(得分:0)

这应该给TextBox一个Grid / Table外观:

while (reader.Read())
{
    textBox1.Text = reader.getString(0)+"\t"+reader.getString(1)+"\r\n"; //in the case the table 'data' has 2 columns
}