如何将从包含一列和多行的查询中检索到的值放入文本框。我的意思是 txtBox1 将具有第一行的值,txtBox2 将具有第二行的值,依此类推。我所知道的是如何检索每列而不是行的值。
最佳答案
也许:
List<TextBox> allTextBoxes = this.Controls.OfType<TextBox>().ToList();
int current = -1;
using (var con = new MySqlConnection(Properties.Settings.Default.ConnectionString))
{
using (var cmd = new MySqlCommand("SELECT ColumnName FROM dbo.TableName", con))
{
con.Open();
using (var rd = cmd.ExecuteReader())
{
while (rd.Read() && ++current < allTextBoxes.Count)
{
allTextBoxes[current].Text = rd.GetString(0);
}
}
}
}
将 this.Controls
替换为包含所有 TextBox
es 的容器控件。
关于c# - 从查询中检索行值到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26214160/