首页 > 代码库 > VS2012 C# 连接MySQL数据库

VS2012 C# 连接MySQL数据库

原则:不安装

1.下载:  https://dev.mysql.com/downloads/connector/net/6.8.html#downloads

技术分享

2.解压

技术分享技术分享

3.添加引用(一个MySql.Data.dll足矣)

技术分享

 

点击“浏览”,到硬盘上去找

技术分享

4.编写代码

using System;using System.Data;using System.Data.Common;using MySql.Data.MySqlClient;namespace CSMySQL {    class Program {        static void Main(string[] args) {            string STR_DB_CONN = "server=localhost;database=test;uid=root;password=root";            DbConnection conn = null;            DbCommand cmd = null;            DbDataReader dr = null;            conn = new MySqlConnection(STR_DB_CONN);            conn.Open();//连接到数据库            try            {                cmd = conn.CreateCommand();                cmd.CommandText = "SELECT * FROM question";                dr = cmd.ExecuteReader();                while (dr.Read())                {                    String t = dr.GetString(0);                    Console.WriteLine(t);                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            finally            {                if (conn.State == ConnectionState.Open)                {                    conn.Close();                }            }        }    }}

 

VS2012 C# 连接MySQL数据库