1.连接MySQL数据库

2.创建一个数据库类
using MySql.Data.MySqlClient;
class SQLDemo{
MySql.Data.MySqlClient.MySqlConnection conn;
public MySqlConnection Connection()
{
string connestStr = "data source = localhost; uid = 用户名; password = 密码; database =数据库;";
MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connestStr);
conn.Open();
this.conn = conn;
return conn;
}
public void Close()
{
this.conn.Close();
}
public MySqlCommand Command(string sql)
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);
MySql.Data.MySqlClient.MySqlDataAdapter sda = new MySql.Data.MySqlClient.MySqlDataAdapter();
sda.SelectCommand = cmd;
return cmd;
}
public int Excute(string sql)
{
return Command(sql).ExecuteNonQuery();
}
public MySqlDataReader read(string sql)
{
return Command(sql).ExecuteReader();
}
}