Home > Software engineering >  C # access MySQL database problems
C # access MySQL database problems

Time:09-22

Recently in learning c # development, need to connect to the database, do a simple test, click on the button, it will display the corresponding table in the database to the DataGridView, online copy a piece of code, and then made the necessary changes, run without error, is to click on the button no response, a great god help


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 error

CodePudding user response:

reference 1st floor Runnerchin 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 database to read and write after


Should have nothing to do with this, I've tried, still won't do
  • Related