简体   繁体   English

C#-Datagridview不显示来自MySQL的数据

[英]C# - Datagridview does not show data from MySQL

I have a datagridview which should contain all users in mysql database. 我有一个datagridview应该包含mysql数据库中的所有用户。 For my problem, it adds rows based on the number of rows in the MySQL, but does not show any data on it. 对于我的问题,它根据MySQL中的行数添加行,但是不显示任何数据。 see image below (i have 2 rows in database): 参见下图(我在数据库中有2行):

screenshot 屏幕截图

Here's my code that I've tried: 这是我尝试过的代码:

using MySql.Data.MySqlClient;

namespace SampleDatabaseQueries
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        MySqlConnection conn;
        MySqlCommand cmd;
        private void Main_Load(object sender, EventArgs e)
        {
            string connString = "server=localhost;user=root;password=;database=test_db";
            conn = new MySqlConnection(connString);

            showData();
        }

        public void showData()
        {

            string query = "SELECT * FROM test_db.users;";
            cmd = new MySqlCommand(query, conn);

            conn.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataTable dTable = new DataTable();
            adapter.Fill(dTable);

            dgUsers.DataSource = dTable;

            conn.Close();
        }
    }
}

EDIT: above code works, data was already loaded but it adds another column so I did not see at first (as you can see the long horizontal scroll on the above screenshot). 编辑:上面的代码有效,数据已经加载,但是它添加了另一列,所以我一开始没有看到(因为您可以看到上面的屏幕截图中的长水平滚动条)。 To solve that, I removed the columns I manually added and changed my query to: 为了解决这个问题,我删除了手动添加的列,并将查询更改为:

SELECT username AS Username, password AS Password FROM test_db.users;

Thank you! 谢谢!

Replace this: 替换为:

MySqlDataAdapter adapter = new MySqlDataAdapter();

With: 带有:

using (MySqlDataAdapter reader = new MySqlDataAdapter(cmd))
{
    adapter.Fill(dTable);
}

EDIT 编辑

For further improvement you could make a database connection class, i suppose you are using the MySql connector from oracle so you can check out their documentation on how to do this. 为了进一步改进,您可以制作一个数据库连接类,我想您正在使用来自oracle的MySql连接器,因此您可以查看他们的文档以了解如何执行此操作。 Here is a link that can get you started on the most basic things like connecting and filling datatables: MySql connector documentation 这是一个可以使您开始最基本的事情的链接,例如连接和填充数据表: MySql连接器文档

EDIT 1 编辑1

As you said my solution did not work, here is another one that I just tested and should work. 正如您所说,我的解决方案无效,这是我刚刚测试过的另一种可行的解决方案。

DataTable dTable = new DataTable();
conn.Open();
string query = "SELECT * FROM test_db.users;";
MysqlCommand cmd = new MySqlCommand(query, conn);
using (MySqlDataAdapter reader = new MySqlDataAdapter(cmd))
{
    adapter.Fill(dTable);
}
dgUsers.DataSource = dTable;
conn.Close();

This should work. 这应该工作。 If the datatable is not showing any rows(Empty rows means it found something), then there is a different problem, maybe your database is empty. 如果数据表未显示任何行(空行表示发现了某行),则存在另一个问题,可能您的数据库为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM