下载后安装
在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中,为了便于大家立马使用,下面自己写了一个真实的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Odbc;
using MySQLDriverCS;
public class MySQLDataAccessFactory
{
private const string connstr = "Data Source=数据库名称;Password=数据库密码;User ID=数据库用户名;Location=数据库服务器ip或机器名";
private MySQLConnection conn = null;
private MySQLCommand command = null;
private MySQLConnection GetConnection()
{
if (conn != null)
{
conn.Open();
return conn;
}
else
{
conn = new MySQLConnection(connstr);
conn.Open();
return conn;
}
}
private void CloseConnection()
{
try
{
if (conn != null)
{
conn.Close();
conn.Dispose();
conn = null;
}
}
catch (Exception ex)
{
throw new Exception("MySQL数据库连接失败", ex);
}
}
//防止发生中文乱码
private void KillChinese()
{
MySQLCommand mysql_command = new MySQLCommand("set names gb2312", GetConnection());
mysql_command.ExecuteNonQuery();
}
//得到command
public MySQLCommand GetCommand(string cmdText)
{
if (command != null)
{
return command;
}
else
{
command = new MySQLCommand(cmdText, GetConnection());
return command;
}
}
//添加参数
public void AddInParameter(MySQLCommand command, string name, object value)
{
IDataParameter param = command.CreateParameter();
param.ParameterName = name;
if (value
1