using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsDataAdapter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } MySqlConnection conn; private void button1_Click(object sender, EventArgs e) { conn = new MySqlConnection("server=localhost;database=csharp;uid=root;pwd=abc123"); //创建Command对象 MySqlCommand cmd = new MySqlCommand("select * from tb_command", conn); //创建适配器 MySqlDataAdapter sda = new MySqlDataAdapter(); //SelectCommand是用来向数据库发送select语句 sda.SelectCommand = cmd; DataSet ds = new DataSet(); sda.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables[0]; } } }