Form1.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. namespace WindowsFormsDataSetCopy
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. MySqlConnection conn;
  21. DataSet ds;
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. conn = new MySqlConnection("server=localhost;database=csharp;uid=root;pwd=abc123");
  25. MySqlCommand cmd = new MySqlCommand("select * from tb_test", conn);
  26. MySqlDataAdapter sda = new MySqlDataAdapter();
  27. sda.SelectCommand = cmd;
  28. ds = new DataSet();
  29. sda.Fill(ds, "test");
  30. dataGridView1.DataSource = ds.Tables[0];
  31. }
  32. private void button1_Click(object sender, EventArgs e)
  33. {
  34. //复制
  35. DataSet ds1 = ds.Copy();
  36. dataGridView2.DataSource = ds1.Tables[0];
  37. }
  38. }
  39. }