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; using System.Xml.Linq; namespace WindowsFormsDataAdapter2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } MySqlConnection conn; DataSet ds; MySqlDataAdapter sda; private void Form1_Load(object sender, EventArgs e) { conn = new MySqlConnection("server=localhost;database=csharp;uid=root;pwd=abc123"); MySqlCommand cmd = new MySqlCommand("select * from tb_command", conn); //创建适配器 sda = new MySqlDataAdapter(); sda.SelectCommand = cmd; //创建数据集 ds = new DataSet(); //将MySqlDataAdapter数据管道中的数据传输到DataSet中 sda.Fill(ds, "cs"); //将数据绑定到DataGridView控件上 dataGridView1.DataSource = ds.Tables[0]; } private void button1_Click(object sender, EventArgs e) { ////从数据集中取出名称为cs的内存表 //DataTable dt = ds.Tables["cs"]; ////调用FillSchema获取表的数据结构 //sda.FillSchema(dt, SchemaType.Mapped); ////使用datatable内存表查找指定编号的数据记录 //DataRow dr = dt.Rows.Find(txtNo.Text); //dr["username"] = txtName.Text.Trim(); //dr["sex"] = this.txtSex.Text.Trim(); //dr["age"] = this.txtAge.Text.Trim(); //dr["bonus"] = this.txtBonus.Text.Trim(); ////创建cmdbuider对象,关联UpdateCommand属性,实现内部更新命令的关联 //MySqlCommandBuilder cmdbuider = new MySqlCommandBuilder(sda); ////实现更新数据库 //sda.Update(dt); } //CellClick:某一个单元格被单击时触发 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString(); txtName.Text = dataGridView1.SelectedCells[1].Value.ToString(); txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString(); txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString(); txtBonus.Text = dataGridView1.SelectedCells[4].Value.ToString(); } } }