用 MySQL 数据填充 Datagridview [英] Fill Datagridview with MySQL data

查看:32
本文介绍了用 MySQL 数据填充 Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几次使其工作,但它只是没有用 mysql 数据填充 datagridview,这是我的代码:

i tried several times to make it work but it just doesn't fill up the datagridview with mysql data ,here's my code :

string connectionString = "SERVER=localhost;DATABASE=shootsource;UID=root;PASSWORD=;";
        string sql = "SELECT * FROM characters";
        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
        sCommand = new MySqlCommand(sql, connection);
        sAdapter = new MySqlDataAdapter(sCommand);
        sBuilder = new MySqlCommandBuilder(sAdapter);
        sDs = new DataSet();
        sAdapter.Fill(sDs, "characters");
        sTable = sDs.Tables["characters"];
        connection.Close();
        dataGridView1.DataSource = sDs.Tables["characters"];
        dataGridView1.ReadOnly = true;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

它仅显示 mysql 表字符"中的列:id、name、time 等...我如何从数据库中填充它?

It shows only the columns from the mysql table 'characters' : id,name,time etc...how can i populate it from the db?

这是问题的图像:

推荐答案

       private void MySQL_ToDatagridview()
    {
        //VarribleKeeper.MySQLConnectionString = your connection string
       //info being your table name
        MySqlConnection mysqlCon = new  

        MySqlConnection(VarribleKeeper.MySQLConnectionString);
        mysqlCon.Open();

        MySqlDataAdapter MyDA = new MySqlDataAdapter();
        string sqlSelectAll = "SELECT * from info";
        MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

        DataTable table = new DataTable();
        MyDA.Fill(table);

        BindingSource bSource = new BindingSource();
        bSource.DataSource = table;


        dataGridView1.DataSource = bSource;
    }

这篇关于用 MySQL 数据填充 Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆