Cat*_*hai 4 c# mysql datagridview
我尝试了几次使它工作,但它只是没有用mysql数据填充datagridview,这是我的代码:
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;
Run Code Online (Sandbox Code Playgroud)
它只显示mysql表'characters'中的列:id,name,time等...我如何从db填充它?
这是问题的图像:

Tac*_*co2 10
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21713 次 |
| 最近记录: |