在C#文本框中的不同行上显示数据库中的值

本文关键字:显示 数据库 文本 | 更新日期: 2023-09-27 18:25:36

我一直在开发这个程序,它将从数据库中提取帐户,并将它们显示在文本框中。到目前为止,我已经完成了这项工作,但它只会从第一行的数据库中提取该帐户。这是我的代码:

 string myConnection = "datasource=xxxx;port=3306;username=xxxx;password=xxxx!";
        string Query = "select * from xxx.xxx;";
        MySqlConnection conDataBase = new MySqlConnection(myConnection);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;
        DataTable dt = new DataTable();
        try 
        {
            conDataBase.Open();
             myReader = cmdDataBase.ExecuteReader();
             while (myReader.Read())
             {
                 string sUsername = myReader.GetString("usernames");
                 string sPasswords = myReader.GetString("passwords");
                 nsTextBox1.Text = sUsername + ":" + sPasswords;

             }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

还有,无论如何,它会删除旧的用户名+密码,并显示被提取的新用户名+密码吗?

在C#文本框中的不同行上显示数据库中的值

像这样?:

nsTextBox1.Text += sUsername + ":" + sPasswords + Enviroment.NewLine;

此外,您的TextBox需要是多行的。

它一定是一个文本框吗?为什么不使用ListBox?