当前位置:Gxlcms > 数据库问题 > C#使用MysqlBackup.Net 备份MySQL数据库

C#使用MysqlBackup.Net 备份MySQL数据库

时间:2021-07-01 10:21:17 帮助过:4人阅读

MySql.Data.dll 技术分享    好了  我们开始调用 我们参考官方的代码实例   C#调用MySqlBackup.dll 备份Mysql数据库
  1.  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数据库

标签:

人气教程排行