首先 聲明這篇博文沒有什么技術水平 希望各位大神口下留情
好的 咱們進入主題
開場介紹一下這個MysqlBackup.Net dll
是國外開源的作品 官方網站
https://mysqlbackupnet.codeplex.com/
我這里提供目前官方最新版本 2.0.9.2 解壓縮后里面會有兩個文件夾 binaries里面是類庫 里面分各個.net版本 source code里面是源代碼 有興趣的同學可自行下載研究其代碼


因為我項目版本是4.0的 所以要引用4.0的庫 當然引用2.0 和3.5也是沒問題的
這里要提醒一下:MySqlBackup.dll是依賴於MySql.Data.dll的 所以引用
MySqlBackup.dll的同時也要引用
MySql.Data.dll

好了 我們開始調用 我們參考官方的代碼實例
C#調用
MySqlBackup.dll
備份Mysql數據庫
-
1 string constring = "server=localhost;user=root;pwd=qwerty;database=test;"; 2 string file = "C:\\backup.sql"; 3 using (MySqlConnection conn = new MySqlConnection(constring)) 4 { 5 using (MySqlCommand cmd = new MySqlCommand()) 6 { 7 using (MySqlBackup mb = new MySqlBackup(cmd)) 8 { 9 cmd.Connection = conn; 10 conn.Open(); 11 mb.ExportToFile(file); 12 conn.Close(); 13 } 14 } 15 }
C#調用
MySqlBackup.dll
還原
Mysql數據庫
1 string constring = "server=localhost;user=root;pwd=qwerty;database=test;"; 2 string file = "C:\\backup.sql"; 3 using (MySqlConnection conn = new MySqlConnection(constring)) 4 { 5 using (MySqlCommand cmd = new MySqlCommand()) 6 { 7 using (MySqlBackup mb = new MySqlBackup(cmd)) 8 { 9 cmd.Connection = conn; 10 conn.Open(); 11 mb.ImportFromFile(file); 12 conn.Close(); 13 } 14 } 15 }
調用看起來很簡單
但是現實總比我們想象中要骨干的多
樓主在調用
MySqlBackup.dll 備份Discuz數據庫時 遇到了如下錯誤

這錯誤讓樓主百撕不得騎姐
百度上基本查不到這個錯誤 只搜到一個靠譜點的 搜索關鍵字 [C# MySQL GUID應包含4個短划線的32位數]
看了這位博主的解釋 沒太看明白 樓主的領悟力確實不太好
於是不得不再去Google一下 關鍵字為[C# MySQL Guid should contain 32 digits and 4 dashes]
搜到一個頁面 截取里面比較重要的幾句話 樓主英語也不是很好 大家將就着看
That error comes from the MySQL-Connector. Everything that is CHAR(36) in your database will be parsed as GUID in .NET. If there is something as '', null or something that cannot be parsed as GUID the connector throws an Exception.
大概意思是這樣的:
這個錯誤是由
MySQL-Connector引起的(
MySql.Data.dll
),MySQL中所有char(36)格式的字段都將被處理成.net中的GUID類型.
如果char(36)字段內容為 '' null 或者其他不能被轉換成GUID類型的東東都將拋出異常.
結合剛才那位博主寫的內容 貌似有些明白 大概我們知道問題出在哪了 那應該怎么解決呢 這篇文章 也提供了我們幾種解決方案
We chose to declare char(36) as always containing guids. If your column can contain nulls, I suggest you use NULL instead of '' to represent that. If the column is not containing guids, then use char(37) or some other length.
Long story short: add the following to your connection-string to parse CHAR(36) as System.String and not as GUID:
old guids=true;
Here is an example MySQL.NET connection String:
server=localhost;user=root;password=abc123;database=test;old guids=true;
解決方法大概意思是這樣的:
1.如果你MySQL數據庫 char(36)字段內容不包含GUID建議更改char長度或者更改為其他類型
2.如果不想更改字段類型可以在鏈接字符串中加入 old guids=true;
根據建議在鏈接字符串中加入
old guids=true;后 char(36)將被當做字符串來處理
重新生成 再次調試 ok 一遍通過
第一次寫博文略顯啰嗦 但是這里樓主想強調的是解決問題的思路.
凡事還是多琢磨 多動手 多實踐 才會進步
如果有大神覺得上面言論有不妥的地方 歡迎隨時拍磚
好的 今天的逼就裝的這里 謝謝大家
