using System;
using System.Collections.Generic;
Using System.Com ponentModel;
using System.Data;
Using System. Drawing;
using System.Linq;
using System.Text;
Using System. Windows. Forms;
Using MySql. Data. MySqlClient;
using System.Data.SqlClient;
Using MySQLDriverCS;
The namespace DbTest
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent();
}
Private void button1_Click (object sender, EventArgs e)
{
String strConn="server=localhost; Uid=root; Password=; The Database=test; Charset=utf8 ";
MySqlConnection conn=new MySqlConnection (strConn);
conn.Open();//links database
Try
{
conn.Open();
conn.Close();
MessageBox. Show (" database connection success!" );
}
The catch (Exception)
{
MessageBox. Show (" connect to database failure!" );
}
DataSet ds=new DataSet();
String SQL="select * from student";
MySqlDataAdapter da=new MySqlDataAdapter (SQL, conn);
Da. The Fill (ds, "table");
DataGridView1. The DataSource=ds. The Tables (" table ");
}
}
}
CodePudding user response:
try
{
conn.Open();
conn.Close();
MessageBox. Show (" database connection success!" );
}
The catch (Exception)
{
MessageBox. Show (" connect to database failure!" );
}
Is open to open the connection, the close is close the connection, the connection just open has been closed, you should put the close this after database, speaking, reading and writing
CodePudding user response:
Should be and it doesn't matter, I tried, or useless, very depressed, obviously not an errorCodePudding user response: