Form1.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Xml.Linq;
  13. namespace WindowsFormsDataAdapter2
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. MySqlConnection conn;
  22. DataSet ds;
  23. MySqlDataAdapter sda;
  24. private void Form1_Load(object sender, EventArgs e)
  25. {
  26. conn = new MySqlConnection("server=localhost;database=csharp;uid=root;pwd=abc123");
  27. MySqlCommand cmd = new MySqlCommand("select * from tb_command", conn);
  28. //创建适配器
  29. sda = new MySqlDataAdapter();
  30. sda.SelectCommand = cmd;
  31. //创建数据集
  32. ds = new DataSet();
  33. //将MySqlDataAdapter数据管道中的数据传输到DataSet中
  34. sda.Fill(ds, "cs");
  35. //将数据绑定到DataGridView控件上
  36. dataGridView1.DataSource = ds.Tables[0];
  37. }
  38. private void button1_Click(object sender, EventArgs e)
  39. {
  40. ////从数据集中取出名称为cs的内存表
  41. //DataTable dt = ds.Tables["cs"];
  42. ////调用FillSchema获取表的数据结构
  43. //sda.FillSchema(dt, SchemaType.Mapped);
  44. ////使用datatable内存表查找指定编号的数据记录
  45. //DataRow dr = dt.Rows.Find(txtNo.Text);
  46. //dr["username"] = txtName.Text.Trim();
  47. //dr["sex"] = this.txtSex.Text.Trim();
  48. //dr["age"] = this.txtAge.Text.Trim();
  49. //dr["bonus"] = this.txtBonus.Text.Trim();
  50. ////创建cmdbuider对象,关联UpdateCommand属性,实现内部更新命令的关联
  51. //MySqlCommandBuilder cmdbuider = new MySqlCommandBuilder(sda);
  52. ////实现更新数据库
  53. //sda.Update(dt);
  54. }
  55. //CellClick:某一个单元格被单击时触发
  56. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  57. {
  58. txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
  59. txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
  60. txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString();
  61. txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString();
  62. txtBonus.Text = dataGridView1.SelectedCells[4].Value.ToString();
  63. }
  64. }
  65. }