Microsoft's Visual Studio does not have its own driver to connect to MySQL, to download a mysql-connector-net-6.4.3 driver on the Internet, and then install it to use.
The following is a class that I encapsulate to connect the database, call directly.
Copy Code code 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>
Getting the database connection bridge
</summary>
private static Mysqlconnection Connection
{
Get
{
String connectionString = configurationmanager.appsettings["connectionString"];
String connectionString = "Server=localhost;user id=root; Password=root; Database=system; Pooling=false ";
server=222.222.222.222;port=3306;uid=user;pwd=;d atabase=basename; remote 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>
Get 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>
Executes the SQL statement, returning a row of columns ...
</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 perform a deletion, you can call Getconnection.noselect ("delete from UserInfo where id=1"), read one of the tables in the database, and you can call getconnection.getdatatable ("SELECT * from UserInfo"); it's convenient to call.