1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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 MySql.Data.MySqlClient;
- namespace WindowsFormsSqlConnectionClose
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- MySqlConnection conn;
- private void button1_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- {
- MessageBox.Show("请输入数据库名称");
- }
- else
- {
- try
- {
- string str = "server=localhost;database=" + textBox1.Text.Trim() + ";uid=root;pwd=abc123";
- conn = new MySqlConnection(str);
- conn.Open();
- if (conn.State == ConnectionState.Open)
- {
- MessageBox.Show("连接成功");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- textBox1.Text = "";
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- string str = "";
- conn.Close();
- if (conn.State == ConnectionState.Closed)
- {
- str = "数据库已经成功关闭\n";
- }
- conn.Open();
- if (conn.State == ConnectionState.Open)
- {
- str += "数据库已经成功打开\n";
- }
- richTextBox1.Text = str;
- }
- catch (Exception ex)
- {
- richTextBox1.Text = ex.Message;
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- try
- {
- conn.Dispose();
- conn.Open();
- }
- catch (Exception ex)
- {
- richTextBox1.Text = ex.Message;
- }
- }
- }
- }
|