我有一个大约有40行的MySQL数据库。
我想将“值”分别读入自己的文本框中。
到目前为止我有什么
using (var cmd2 = new OdbcCommand("select value,entity_id,store_id from catalog_category_entity_varchar where attribute_id = 41 ", con))
{
con.Open();
using (var reader = cmd2.ExecuteReader())
{
while (reader.HasRows)
{
int count = reader.FieldCount;
for (int i = 0; i < count; i++)
{
while (reader.Read())
{
textBox[i].txt = (reader[0].ToString());
this.Controls.Add(textBox[i].txt);
reader.NextResult();
}
}
}
我只将第一条记录放入文本框中。
假设行数未知。 如果有40行,则必须有40个文本框。
我已将我的代码编辑为:
using (var reader = cmd2.ExecuteReader())
{
int i = 0;
while (reader.HasRows)
{
TextBox txt = new TextBox();
txt.Text = reader.GetString(0);
this.Controls.Add(txt);
textBox[i].txt = (reader.GetString(0));
this.Controls.Add(textBox[i].txt);
reader.NextResult();
i++;
}
}
我得到的错误是:错误 - 当前上下文中不存在名称文本框
答案 0 :(得分:0)
如果您的textBox
是一系列控件
con.Open(); -- you need open the connection before the command
using (var cmd2 = new OdbcCommand("select value,entity_id,store_id
from catalog_category_entity_varchar
where attribute_id = 41 ", con))
{
using (var reader = cmd2.ExecuteReader())
{
int i = 0;
while (reader.Read())
{
TextBox txt = new TextBox();
txt.Text = reader.GetString(0);
txt.Location.x = 100;
txt.Location.y = 100 * i;
this.Controls.Add(txt);
i++;
}
如果您还没有创建文本框,则需要
TextBox txt = new TextBox();
txt.Text = reader.GetValue(0);
txt.Location.x = 100;
txt.Location.y = 100 * i;
this.Controls.Add(txt);