Form1.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using MySql.Data.MySqlClient;
  12. namespace WindowsFormsCommand
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. MySqlConnection conn;
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. conn = new MySqlConnection("server=localhost;database=dingdingeng;uid=root;pwd=abc123");
  24. conn.Open();
  25. }
  26. private void button1_Click(object sender, EventArgs e)
  27. {
  28. try
  29. {
  30. if (conn.State == ConnectionState.Open || textBox1.Text != "")
  31. {
  32. MySqlCommand cmd = new MySqlCommand();
  33. cmd.Connection = conn;
  34. cmd.CommandText = "select count(*) from " + textBox1.Text.Trim();
  35. cmd.CommandType = CommandType.Text;
  36. int i = Convert.ToInt32(cmd.ExecuteScalar());
  37. label2.Text = "数据表中共有:" + i.ToString() + "条数据";
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. MessageBox.Show(ex.Message);
  43. }
  44. }
  45. }
  46. }