12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- 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 WindowsFormsSqlConnection
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- {
- MessageBox.Show("请输入要连接的数据库名称");
- }
- else
- {
- try
- {
- string ConStr = "server=localhost;database=" + textBox1.Text.Trim() + ";uid=root;pwd=abc123";
- //创建数据库连接对象
- //SqlConnection conn = new SqlConnection(ConStr);
- MySqlConnection conn = new MySqlConnection(ConStr);
- conn.Open();//通过Open方法打开数据库连接
- if (conn.State == ConnectionState.Open)
- {
- label2.Text = "数据库【" + textBox1.Text.Trim() + "】已经连接并打开";
- }
- }
- catch
- {
- MessageBox.Show("连接数据库失败");
- }
- }
- }
- }
- }
|