在 Visual C# 中从 MySQL (phpmyadmin) 检索数据到 TextBox
我已经发布了几个与此相关的问题,但没有一个真正对我有帮助...这里我有一个更清晰的解释:
我将数据放入SQL表中,这是输入的数据类型(所有数据都是字符串类型) : http://i40.tinypic.com/33kaoat.png
当我点击“提交”按钮时 -当我从 PhpMyAdmin 检查数据时,数据保存在表中。但现在我想在单击“刷新”按钮时将此数据检索到下一个选项卡表单中: http:// i41.tinypic.com/34hdtv4.png
textBox5 是我希望在单击“刷新”按钮后显示数据的文本框
这是我迄今为止为“刷新”按钮,但它给了我一个错误:
private void button3_Click(object sender, EventArgs e)
{
string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
using (MySqlConnection mcon = new MySqlConnection(connString))
using (MySqlCommand cmd = mcon.CreateCommand())
{
mcon.Open();
cmd.CommandText = "SELECT * FROM requesttcw";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `ID`=[value-1]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `ClanName`=[value-2]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `Date`=[value-3]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `Type`=[value-4]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `Rules`=[value-5]");
this.textBox5.Text = " || ";
}
reader.Close();
}
mcon.Close();
}
}
我希望显示数据的文本框称为textBox5。
I've posted several questions regarding this but none of them really helped me... Here I go with a clearer explanation:
I put data into the SQL table, here is what type of data goes in (All of them are String type):
http://i40.tinypic.com/33kaoat.png
When I click "Submit" button - the data saves in the table when I check it from PhpMyAdmin. But now I want to retrieve this data into this next tab form when I click "Refresh" button: http://i41.tinypic.com/34hdtv4.png
textBox5 is the textbox which I want my data to show up after I click on "Refresh" button
Here is the script I've done so far for the "Refresh" Button, but it gives me an error:
private void button3_Click(object sender, EventArgs e)
{
string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
using (MySqlConnection mcon = new MySqlConnection(connString))
using (MySqlCommand cmd = mcon.CreateCommand())
{
mcon.Open();
cmd.CommandText = "SELECT * FROM requesttcw";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `ID`=[value-1]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `ClanName`=[value-2]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `Date`=[value-3]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `Type`=[value-4]");
this.textBox5.Text = " || ";
this.textBox5.Text = reader.GetString("UPDATE `requesttcw` SET `Rules`=[value-5]");
this.textBox5.Text = " || ";
}
reader.Close();
}
mcon.Close();
}
}
The textbox I want the data to showin is called textBox5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不能使用 ListBox 来显示字符串?
已编辑
我假设您正在更新表格一次,并且您想要选择更新的行并显示项目。如果是这种情况,请将表更新为:
之后,运行选择查询并初始化 DataReader。使用 DataReader:
这里我假设您选择单行。如果你想连续选取行并持续显示,那么你需要使用ListBox。
Why can't you use a ListBox instead to show the string?
Edited
I assume that you are updating the table once and than you want to pick the updated row and show the items. If this is the case, update the table as:
After that, run your select query and initialize the DataReader. With DataReader:
Here I am assuming that you are picking a single row. If you want to continuously pick rows and keep showing, then you need to use ListBox.