时间:2021-07-01 10:21:17 帮助过:4人阅读
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 }
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数据库时 遇到了如下错误
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
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;
C#使用MysqlBackup.Net 备份MySQL数据库
标签: