1. 程式人生 > >VS2012 C# 連接MySQL數據庫

VS2012 C# 連接MySQL數據庫

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數據庫