填充的DataGridView与MySQL数据 [英] Fill Datagridview with MySQL data

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

问题描述

我试了几次,使其工作,但它只是不填充与MySQL数据DataGridView的,这里是我的代码:

 字符串的connectionString =服务器=本地主机; DATABASE = shootsource; UID =根;密码=;; 
字符串SQL =SELECT * FROM人物;
的MySqlConnection连接=新的MySqlConnection(的connectionString);
connection.Open();
sCommand =新的MySqlCommand(SQL,连接);
sAdapter =新MySqlDataAdapter的(sCommand);
sBuilder =新的MySqlCommandBuilder(sAdapter);
SDS =新的DataSet();
sAdapter.Fill(SDS,字);
=稳定sDs.Tables [字符];
的Connection.close();
dataGridView1.DataSource = sDs.Tables [字符];
dataGridView1.ReadOnly = TRUE;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;



这表明仅仅从MySQL表'人物'的列:ID,姓名,时间等。 ?。如何我可以从数据库中填充它。



这是问题的一个形象:




解决方案

 私人无效MySQL_ToDatagridview()
{
//VarribleKeeper.MySQLConnectionString =连接字符串
//信息是你的表名
的MySqlConnection mysqlCon =新的

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

MySqlDataAdapter的MyDA =新MySqlDataAdapter的();
串sqlSelectAll =SELECT * FROM信息;
MyDA.SelectCommand =新的MySqlCommand(sqlSelectAll,mysqlCon);

DataTable的表=新的DataTable();
MyDA.Fill(表);

的BindingSource bSource =新的BindingSource();
bSource.DataSource =表;


dataGridView1.DataSource = bSource;
}


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;

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

Here is an image of the problem:

解决方案

       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;
    }

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

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