原则:不安装
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(); } } } } }
时间: 2024-10-14 23:21:29