1. 程式人生 > >Nuget下載mysql.Data 連線mysql資料庫一

Nuget下載mysql.Data 連線mysql資料庫一

開啟vs專案,右鍵選擇

在搜尋中輸入mysql.Data 然後可以選擇安裝,但是可能會出現這個提示

這是因為版本的問題,所以,自己去官網重新下載,我這裡選擇的是這個版本,

檢視版本

Version Downloads Last updated
91,403 2 months ago
9,233 a month ago
46,819 5 months ago
71,305 5 months ago
6.8.8 52,195 2016/6/27
6.7.9 75,971 2015/10/14

只能選擇最低版本了。

使用Nuget安裝

首選在vs2010中安裝Nuget, 這個在網上有教程。

在控制檯中輸入:

PM> Install-Package MySql.Data -Version 6.7.9

這樣再專案的應用中就出現了MySql.Data.dll檔案了。

這樣就是在專案中安裝了驅動,不用自己在去下載Mysql-Connector的檔案了。

mysql-connector-net-6.5.7.msi  安裝版本的

mysql-connector-net-6.5.7-noinstall.zip,非安裝版本,使用的時候是把裡面的mysql.data.dll放在專案的引用中。

執行程式碼連線資料庫測試。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace Mysql
{
    class Program
    {
        static void Main(string[] args)
        {
            string connecStr = "server=127.0.0.1;port=3306;database=rose;user=root;password=123";
            MySqlConnection conn = new MySqlConnection(connecStr);
            try
            {
                conn.Open();
                Console.WriteLine("連線成功!");
 
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();

        }
    }
}