在 Visual C# 中从 MySQL (phpmyadmin) 检索数据到 TextBox

发布于 2025-01-05 22:36:25 字数 1880 浏览 0 评论 0原文

我已经发布了几个与此相关的问题,但没有一个真正对我有帮助...这里我有一个更清晰的解释:

我将数据放入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2025-01-12 22:36:25

为什么不能使用 ListBox 来显示字符串?

ListBox.Items.Add(<Your DataReader String>);

已编辑

我假设您正在更新表格一次,并且您想要选择更新的行并显示项目。如果是这种情况,请将表更新为:

UPDATE requesttcw SET 
  ID=value-1, 
  ClanName = Value-2,
  Date = value-3,
  Type = value-4,
  Rules = value-5

之后,运行选择查询并初始化 DataReader。使用 DataReader:

string StringToShow = dr[0] + "||" + dr[1] .....    
textBox5.Text = StringToShow;

这里我假设您选择单行。如果你想连续选取行并持续显示,那么你需要使用ListBox。

Why can't you use a ListBox instead to show the string?

ListBox.Items.Add(<Your DataReader 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:

UPDATE requesttcw SET 
  ID=value-1, 
  ClanName = Value-2,
  Date = value-3,
  Type = value-4,
  Rules = value-5

After that, run your select query and initialize the DataReader. With DataReader:

string StringToShow = dr[0] + "||" + dr[1] .....    
textBox5.Text = StringToShow;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文