填写Datagridview与MySQL数据 [英] Fill Datagridview with MySQL data

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

问题描述

我尝试过几次使其正常工作,但它并没有填满datagridview与mysql数据,这里是我的代码:

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

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

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