C#连接MySql数据库的方法-用MySQLDriverCS连接MySQL数据库。
先下载和安装MySQLDriverCS,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中。
注:我下载的是版本是 MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
C#连接MySql数据库代码:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.ComponentModel;
- usingSystem.Data;
- usingSystem.Data.Odbc;
- usingSystem.Drawing;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Windows.Forms;
- usingMySQLDriverCS;
- namespacemysql{
- publicpartialclassForm1:Form{
- publicForm1(){
- InitializeComponent();
- }
- privatevoidForm1_Load(objectsender,EventArgse){
- MySQLConnectionconn=null;
- conn=newMySQLConnection(newMySQLConnectionString
("localhost","inv","root","831025").AsString);- conn.Open();
- MySQLCommandcommn=newMySQLCommand("setnamesgb2312",conn);
- commn.ExecuteNonQuery();
- stringsql="select*fromexchange";
- MySQLDataAdaptermda=newMySQLDataAdapter(sql,conn);
- DataSetds=newDataSet();
- mda.Fill(ds,"table1");
- this.dataGrid1.DataSource=ds.Tables["table1"];
- conn.Close();
- }
- }
- }
以上介绍C#连接MySql数据库的方法
【编辑推荐】