C# 连接MySQL

来源:互联网 发布:win7系统查看mac地址 编辑:程序博客网 时间:2024/05/17 06:18

      用MySQLDriverCS连接MySQL数据库 :

      C#用MySQLDriverCS连接MySql需要两个库文件:libmySQL.dll  MySQLDriverCS.DLL,然后在项目中添加引用  MySQLDriverCS.DLL,再在程序中添加using MySQLDriverCS;然后就进行MySql操作了,如下操作:

 

   MySQLConnection conn = null;

   conn = new MySQLConnection(new MySQLConnectionString("localhost", "Menu","root", "123").AsString);

   conn.Open();


   MySQLCommand comm = new MySQLCommand("set names gb2312", conn);

   comm .ExecuteNonQuery();


   string sql = "select * from table_level ";

   MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);


   DataSet ds = new DataSet();

   mda.Fill(ds, "table1");


   this.dataGridView1.DataSource = ds.Tables["table1"];

   conn.Close();