第一种方式:
使用MySQLDriverCS.dll连接
MySQLDriverCS软件下载:http://sourceforge.net/projects/mysqldrivercs/?source=typ_redirect
安装完之后再引用中添加引用,找到安装目录,找到MySQLDriverCS.dll文件,然后添加using MySQLDriverCS.dll文件
参考网址:http://www.cnblogs.com/genli/articles/1956537.html
C#连接mysql代码
MySQLConnection DBConn;
DBConn = new MySQLConnection(new MySQLConnectionString("10.99.19.121","haha", "root", "root", 3306).AsString);
//DBConn = new MySQLConnection(new MySQLConnectionString("数据源","数据库名", "用户名", "密码", 端口号).AsString);
try
{
DBConn.Open(); // 执行查询语句
MessageBox.Show("数据库已经连接了!");
string sql = "select * from tb_user";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();
或者这么写:
MySQLConnectionString constr = new MySQLConnectionString("10.99.19.121", "haha", "root", "root", 3306);
MySQLConnection DBConn = new MySQLConnection(constr.AsString);
//MySQLConnection DBConn;
//DBConn = new MySQLConnection(new MySQLConnectionString("10.99.19.121","haha", "root", "root", 3306).AsString);
try
{
DBConn.Open(); // 执行查询语句
MessageBox.Show("数据库已经连接了!");
string sql = "select * from tb_user";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();
第二种方法:
使用MySql.Data.dll连接
参考网址:http://www.cnblogs.com/sosoft/p/3906136.html
使用过程
dll文件修复方法:
1、解压下载的文件。
2、复制文件“mysql.data.dll”到系统目录下。
3、系统目录一般为:C:\WINNT\System32 64位系统为C:\Windows\SysWOW64
4、最后点击开始菜单-->运行-->输入regsvr32 mysql.data.dll 后,回车即可解决错误提示!
在再引用中添加引用,找到C:\Windows\SysWOW64目录,找到mysql.data.dll文件,然后添加using MySql.Data.MySqlClient;文件
string M_str_sqlcon = "server=10.99.19.121;user id=root;password=root;database=haha"; //根据自己的设置
MySqlConnection mycon = new MySqlConnection();
mycon.ConnectionString = M_str_sqlcon;
try
{
mycon.Open();
MessageBox.Show("数据库已经连接了!");
string sql = "select * from tb_user";
MySqlDataAdapter mda = new MySqlDataAdapter(sql, mycon);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
mycon.Close();
第三种方式:
使用MySQLDriverCS.dll连接
MySQLDriverCS软件下载:http://sourceforge.net/projects/mysqldrivercs/?source=typ_redirect
安装完之后再引用中添加引用,找到安装目录,找到MySQLDriverCS.dll文件,然后添加using MySQLDriverCS.dll文件
参考网址:http://www.cnblogs.com/genli/articles/1956537.html
C#连接mysql代码
MySQLConnection DBConn;
DBConn = new MySQLConnection(new MySQLConnectionString("10.99.19.121","haha", "root", "root", 3306).AsString);
//DBConn = new MySQLConnection(new MySQLConnectionString("数据源","数据库名", "用户名", "密码", 端口号).AsString);
try
{
DBConn.Open(); // 执行查询语句
MessageBox.Show("数据库已经连接了!");
string sql = "select * from tb_user";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();
或者这么写:
MySQLConnectionString constr = new MySQLConnectionString("10.99.19.121", "haha", "root", "root", 3306);
MySQLConnection DBConn = new MySQLConnection(constr.AsString);
//MySQLConnection DBConn;
//DBConn = new MySQLConnection(new MySQLConnectionString("10.99.19.121","haha", "root", "root", 3306).AsString);
try
{
DBConn.Open(); // 执行查询语句
MessageBox.Show("数据库已经连接了!");
string sql = "select * from tb_user";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();
第二种方法:
使用MySql.Data.dll连接
参考网址:http://www.cnblogs.com/sosoft/p/3906136.html
使用过程
dll文件修复方法:
1、解压下载的文件。
2、复制文件“mysql.data.dll”到系统目录下。
3、系统目录一般为:C:\WINNT\System32 64位系统为C:\Windows\SysWOW64
4、最后点击开始菜单-->运行-->输入regsvr32 mysql.data.dll 后,回车即可解决错误提示!
在再引用中添加引用,找到C:\Windows\SysWOW64目录,找到mysql.data.dll文件,然后添加using MySql.Data.MySqlClient;文件
string M_str_sqlcon = "server=10.99.19.121;user id=root;password=root;database=haha"; //根据自己的设置
MySqlConnection mycon = new MySqlConnection();
mycon.ConnectionString = M_str_sqlcon;
try
{
mycon.Open();
MessageBox.Show("数据库已经连接了!");
string sql = "select * from tb_user";
MySqlDataAdapter mda = new MySqlDataAdapter(sql, mycon);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGridView1.DataSource = ds.Tables["table1"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
mycon.Close();
第三种方式:
通过ODBC访问mysql数据库
/** * @sample : mycon.cs * @purpose : Demo sample for ODBC.NET using MyODBC * @author : Venu, <[email protected]> * * (C) Copyright MySQL AB, 1995-2004 * **/ /* build command * * csc /t:exe * /out:mycon.exe mycon.cs * /r:Microsoft.Data.Odbc.dll */ using Console = System.Console; using Microsoft.Data.Odbc; namespace myodbc3 { class mycon { static void Main(string[] args) { try { //Connection string for MyODBC 2.50 /*string MyConString = "DRIVER={MySQL};" + "SERVER=localhost;" + "DATABASE=test;" + "UID=venu;" + "PASSWORD=venu;" + "OPTION=3"; */ //Connection string for MyODBC 3.51 string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=test;" + "UID=venu;" + "PASSWORD=venu;" + "OPTION=3"; //Connect to MySQL using MyODBC OdbcConnection MyConnection = new OdbcConnection(MyConString); MyConnection.Open(); Console.WriteLine("\n !!! success, connected successfully !!!\n"); //Display connection information Console.WriteLine("Connection Information:"); Console.WriteLine("\tConnection String:" + MyConnection.ConnectionString); Console.WriteLine("\tConnection Timeout:" + MyConnection.ConnectionTimeout); Console.WriteLine("\tDatabase:" + MyConnection.Database); Console.WriteLine("\tDataSource:" + MyConnection.DataSource); Console.WriteLine("\tDriver:" + MyConnection.Driver); Console.WriteLine("\tServerVersion:" + MyConnection.ServerVersion); //Create a sample table OdbcCommand MyCommand = new OdbcCommand("DROP TABLE IF EXISTS my_odbc_net",MyConnection); MyCommand.ExecuteNonQuery(); MyCommand.CommandText = "CREATE TABLE my_odbc_net(id int, name varchar(20), idb bigint)"; MyCommand.ExecuteNonQuery(); //Insert MyCommand.CommandText = "INSERT INTO my_odbc_net VALUES(10,'venu', 300)"; Console.WriteLine("INSERT, Total rows affected:" + MyCommand.ExecuteNonQuery());; //Insert MyCommand.CommandText = "INSERT INTO my_odbc_net VALUES(20,'mysql',400)"; Console.WriteLine("INSERT, Total rows affected:" + MyCommand.ExecuteNonQuery()); //Insert MyCommand.CommandText = "INSERT INTO my_odbc_net VALUES(20,'mysql',500)"; Console.WriteLine("INSERT, Total rows affected:" + MyCommand.ExecuteNonQuery()); //Update MyCommand.CommandText = "UPDATE my_odbc_net SET id=999 WHERE id=20"; Console.WriteLine("Update, Total rows affected:" + MyCommand.ExecuteNonQuery()); //COUNT(*) MyCommand.CommandText = "SELECT COUNT(*) as TRows FROM my_odbc_net"; Console.WriteLine("Total Rows:" + MyCommand.ExecuteScalar()); //Fetch MyCommand.CommandText = "SELECT * FROM my_odbc_net"; OdbcDataReader MyDataReader; MyDataReader = MyCommand.ExecuteReader(); while (MyDataReader.Read()) { if(string.Compare(MyConnection.Driver,"myodbc3.dll") == 0) { Console.WriteLine("Data:" + MyDataReader.GetInt32(0) + " " + MyDataReader.GetString(1) + " " + MyDataReader.GetInt64(2)); //Supported only by MyODBC 3.51 } else { Console.WriteLine("Data:" + MyDataReader.GetInt32(0) + " " + MyDataReader.GetString(1) + " " + MyDataReader.GetInt32(2)); //BIGINTs not supported by MyODBC } } //Close all resources MyDataReader.Close(); MyConnection.Close(); } catch (OdbcException MyOdbcException)//Catch any ODBC exception .. { for (int i=0; i < MyOdbcException.Errors.Count; i++) { Console.Write("ERROR #" + i + "\n" + "Message: " + MyOdbcException.Errors[i].Message + "\n" + "Native: " + MyOdbcException.Errors[i].NativeError.ToString() + "\n" + "Source: " + MyOdbcException.Errors[i].Source + "\n" + "SQL: " + MyOdbcException.Errors[i].SQLState + "\n"); } } } } }