1.首先下载连接mysql需要的dll,任意门:http://pan.baidu.com/share/link?shareid=1024071329&uk=2442594409
2.本人选用的时2.0的;
3.然后在工程添加引用如下:
相关类:
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Emergency.DBClient
{
public class MySQLconnection
{
///
/// MySqlConnection连接对象
///
private MySqlConnection connection;
///
/// 服务器地址
///
private string server;
///
/// 数据库实例名称
///
private string database;
///
/// 用户名
///
private string uid;
///
/// 密码
///
private string password;
///
/// 端口号
///
private string port;
public MySqlConnection getInstance() {
return connection;
}
///
/// 初始化mysql连接
///
/// 服务器地址
/// 数据库实例
/// 用户名称
/// 密码
public void Initialize(string server, string database, string uid, string password)
{
this.server = server;
this.uid = uid;
this.password = password;
this.database = database;
//string connectionString = "Data Source=" + server + ";" + "port=" + port + ";" + "Database=" + database + ";" + "User Id=" + uid + ";" + "Password=" + password + ";" + "CharSet = utf8"; ;
string connectionString = "server=" + server + ";user id=" + uid + ";password=" + password + ";database=" + database;
connection = new MySqlConnection(connectionString);
}
///
/// 打开数据库连接
///
/// 是否成功
public bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
Console.Write("Cannot connect to server. Contact administrator");
break;
case 1045:
Console.Write("Invalid username/password, please try again");
break;
}
return false;
}
}
///
/// 关闭数据库连接
///
///
public bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
Console.Write(ex.Message);
return false;
}
}
public MySqlDataAdapter GetAdapter(string SQL)
{
MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
return Da;
}
///
/// 构建SQL句柄
///
/// SQL语句
///
public MySqlCommand CreateCmd(string SQL)
{
MySqlCommand Cmd = new MySqlCommand(SQL, connection);
return Cmd;
}
///
/// 根据SQL获取DataTable数据表
///
/// 查询语句
/// 返回表的表名
///
public DataTable GetDataTable(string SQL, string Table_name)
{
MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
DataTable dt = new DataTable(Table_name);
Da.Fill(dt);
return dt;
}
///
/// 运行MySql语句返回 MySqlDataReader对象
///
///
/// MySqlDataReader对象
public MySqlDataReader GetReader(string SQL)
{
MySqlCommand Cmd = new MySqlCommand(SQL, connection);
MySqlDataReader Dr;
try
{
Dr = Cmd.ExecuteReader(CommandBehavior.Default);
}
catch
{
throw new Exception(SQL);
}
return Dr;
}
///
/// 运行MySql语句,返回DataSet对象
///
/// 查询语句
/// 待填充的DataSet对象
/// 表名
///
public DataSet Get_DataSet(string SQL,DataSet Ds, string tablename)
{
MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
try
{
Da.Fill(Ds, tablename);
}
catch (Exception Ex)
{
throw Ex;
}
return Ds;
}
///
/// 运行MySql语句,返回DataSet对象,将数据进行了分页
///
/// 查询语句
/// 待填充的DataSet对象
/// 开始项
/// 每页数据条数
/// 表名
///
public DataSet GetDataSet(string SQL, DataSet Ds, int StartIndex, int PageSize, string tablename)
{
MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
try
{
Da.Fill(Ds, StartIndex, PageSize, tablename);
}
catch (Exception Ex)
{
throw Ex;
}
return Ds;
}
///
/// 添加数据
///
///
public void getInsert(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("插入数据失败了!" + message);
}
}
///
/// 修改数据
///
///
public static void getUpdate(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("修改数据失败了!" + message);
}
}
///
/// 删除数据
///
///
public static void getDel(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("删除数据失败了!" + message);
}
}
}
}
连接测试:
protected void Application_Start(object sender, EventArgs e) { string server = "localhost"; //服务器 string database = "db"; //数据库实例 string uid = "uid";//用户名 string password = "pwd"; //密码 MySQLconnection mysqlconn = new MySQLconnection(); mysqlconn.Initialize(server,database,uid,password); mysqlconn.OpenConnection(); }