Microsoft's visual studio does not have a built-in connection to Mysql driver, go to the Internet to download a mysql-connector-net-6.4.3 driver, and then the installation can be used.
The following is my encapsulated database connection class, which can be called directly.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data;
Using System. Data. SqlClient;
Using System. Configuration;
Using MySql. Data. MySqlClient;
Namespace DAL
{
Public class GetConnection
{
Private static MySqlConnection _ connection;
/// <Summary>
/// Obtain the Database Connection Bridge
/// </Summary>
Private static MySqlConnection Connection
{
Get
{
// String connectionString = ConfigurationManager. etettings ["ConnectionString"];
String connectionString = "server = localhost; user id = root; password = root; database = system; pooling = false ";
// Server = 222.222.222.222; port = 3306; uid = user; pwd =; database = basename; remotely connected
// String connectionString = "Data Source = 202.192.72.22; Initial Catalog = wwj; Persist Security Info = True; User ID = wwj; Password = wwj123 ";
If (_ connection = null)
{
_ Connection = new MySqlConnection (connectionString );
_ Connection. Open ();
}
If (_ connection. State = ConnectionState. Closed)
{
_ Connection. Open ();
}
If (_ connection. State = ConnectionState. Broken)
{
_ Connection. Close ();
_ Connection. Open ();
}
Return GetConnection. _ connection;
}
}
/// <Summary>
/// Obtain table data
/// </Summary>
/// <Param name = "SQL"> </param>
/// <Returns> </returns>
Public static MySqlDataReader GetDataRead (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
MySqlDataReader read = command. ExecuteReader ();
Return read;
}
Public static int NoSelect (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
Int row = command. ExecuteNonQuery ();
Return row;
}
Public static DataTable GetDataTable (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
DataTable dt = new DataTable ();
MySqlDataAdapter sda = new MySqlDataAdapter (command );
Sda. Fill (dt );
Return dt;
}
/// <Summary>
/// Execute the SQL statement and return one row and one column ..
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Returns> </returns>
Public static string GetScalar (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
Return command. ExecuteScalar (). ToString ();
}
}
}
For example, if you want to delete the object, you can call GetConnection. noSelect ("delete from UserInfo where Id = 1"); read a table in the database and call GetConnection. getDataTable ("select * from UserInfo"); easy to call.