前置條件
- Visual Studio 2013 (專業版/企業版/社區版)
- 必裝:MySQL for Visual Studio 1.1.1+ (提供 DDEX Provider,讓 VS 識別 MySQL)
- MySQL Connector/NET 6.9+ (ADO.NET 驅動,運行時必需)
安裝步驟
- 下載 MySQL for Visual Studio (官網或鏡像站),雙擊安裝,全程 Next
- 下載 MySQL Connector/NET (msi 安裝包),安裝時選擇 Typical
- 重啟 Visual Studio 2013
添加 MySQL 數據源連接 (Server Explorer)
- 打開 **Server Explorer** (伺服器總管) → 右鍵 **Data Connections** → **Add Connection**
- Change... (更改數據源) → 選擇 **MySQL Database** → OK (若無此選項,說明 MySQL for VS 未安裝成功)
- 填寫連接屬性:
| 欄位 | 說明 | 範例 |
| Server name | MySQL 主機地址 | 127.0.0.1 / localhost / 192.168.1.100 |
| Port | 端口 (Advanced 進階中設定) | 3306 |
| User name | 資料庫用戶名 | dev_user |
| Password | 密碼 | ****** |
| Database name | 下拉選擇具體資料庫 | my_project_db |
- 點擊 **Test Connection** → 顯示 "Test connection succeeded" 即成功
- 確定 → Server Explorer 左側展開連接節點,可見 Tables、Views、Stored Procedures、Functions
常見問題排查
| 現象 | 原因 | 解決 |
| Add Connection 列表無 MySQL Database | MySQL for Visual Studio 未安裝或版本不匹配 | 重新安裝對應 VS 版本的 MySQL for VS,確認註冊表 `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\12.0\DataProviders` 有 MySQL 項 |
| Test Connection 失敗:Unable to connect | 網路不通、防火牆、MySQL bind-address、用戶無遠程權限 | ping/telnet 測通、MySQL `GRANT ALL ON db.* TO 'user'@'%' IDENTIFIED BY 'pwd';`、`bind-address=0.0.0.0` |
| 連接成功但展開 Tables 報錯 | Connector/NET 版本過低、SSL/TLS 協商失敗 | 升級 Connector/NET 至 8.0+,連接字串加 `SslMode=None` 或 `SslMode=Preferred` |
| Entity Framework 無法生成模型 | EF 版本與 Connector/NET 不兼容 | VS2013 內建 EF6,需 Connector/NET 6.10+ 或 8.0+,NuGet 安裝 `MySql.Data.Entity` |
進階:連接字串模板 (App.config / Web.config)
<connectionStrings>
<add name="MySqlConn"
connectionString="Server=127.0.0.1;Port=3306;Database=my_db;Uid=dev_user;Pwd=password;Charset=utf8mb4;SslMode=None;Allow User Variables=True;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".NET Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
總結
VS2013 整合 MySQL 關鍵在於 **MySQL for Visual Studio (DDEX) + Connector/NET (Runtime)** 雙組件安裝。配置連接時注意防火牆、遠程授權、SSL 模式、字符集。後續開發建議使用 Entity Framework Core / Dapper 等現代 ORM,而非舊版 DataSet/TableAdapter。