VS2012 C# 連接MySQL數據庫
阿新 • • 發佈:2017-06-17
bsp spa write ogr 代碼 net rar images div
原則:不安裝
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數據庫