標籤:
首先 聲明這篇博文沒有什麼技術水平 希望各位大神口下留情
好的 咱們進入主題
開場介紹一下這個MysqlBackup.Net dll
是國外開源的作品 官方網站 https://mysqlbackupnet.codeplex.com/我這裡提供目前官方最新版本 2.0.9.2 解壓縮后里面會有兩個檔案夾 binaries裡面是類庫 裡面分各個.net版本 source code裡面是原始碼 有興趣的同學可自行下載研究其代碼 點擊下載MysqlBackup.Net 因為我項目版本是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位元]http://www.cnblogs.com/end/archive/2012/12/26/2834068.html看了這位博主的解釋 沒太看明白 樓主的領悟力確實不太好 於是不得不再去Google一下 關鍵字為[C# MySQL Guid should contain 32 digits and 4 dashes]搜到一個頁面 截取裡面比較重要的幾句話 樓主英語也不是很好 大家將就著看原頁面地址 http://www.christianroessler.net/tech/2015/c-sharp-mysql-connector-guid-should-contain-32-digits-with-4-dashes.html
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.
See https://bugs.mysql.com/bug.php?id=60945
大概意思是這樣的:這個錯誤是由 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 一遍通過 第一次寫博文略顯囉嗦 但是這裡樓主想強調的是解決問題的思路.凡事還是多琢磨 多動手 多實踐 才會進步如果有大神覺得上面言論有不妥的地方 歡迎隨時拍磚 好的 今天的逼就裝的這裡 謝謝大家
C#使用MysqlBackup.Net 備份MySQL資料庫