这里是利用MySqlBackup,可以再nuget中下载。
无法解决视图嵌视图报错的问题,只导表跟数据比较合适,如果有视图嵌视图,请参照Mysql备份迁移——Mysqldump(.NET调用Mysqldump.exe方式)——(解决视图嵌视图报错)http://www.cnblogs.com/pbys/p/4142367.html
string constring = "server=192.168.0.1;port=3306;user=root; pwd=123456; database=test1;allow zero datetime = true;"; string file = "C:\\backup.sql"; System.IO.MemoryStream ms = new System.IO.MemoryStream();//导出到内存的方式 using (MySqlConnection conn = new MySqlConnection(constring)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = conn; conn.Open(); //mb.ExportInfo.AddCreateDatabase = true;//是否需要创建数据库 //mb.ExportInfo.ExportTableStructure = true;//导出表结构 //mb.ExportInfo.ExportRows = true;//导出数据 mb.ExportInfo.ExportViews = false;//视图不导 mb.Command.CommandTimeout = 60; mb.ExportToFile(file);//导出到文件的方式 //mb.ExportToMemoryStream(ms);//导出到内存的方式 conn.Close(); } } } constring = "server=192.168.0.2;port=3306;user=root; pwd=123456; database=test1;allow zero datetime = true;"; using (MySqlConnection conn = new MySqlConnection(constring)) { using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = conn; conn.Open(); //mb.ImportInfo.TargetDatabase = "test2"; //mb.ImportInfo.DatabaseDefaultCharSet = "utf8"; mb.Command.CommandTimeout = 60; mb.ImportFromFile(file);//导出到文件的方式 //mb.ImportFromMemoryStream(ms);//导出到内存的方式 conn.Close(); } } }
更多参数及更新请参考http://www.codeproject.com/Articles/256466/MySqlBackup-NET