如何在C#.NET中备份MYSQL数据库 [英] how to take backup of the MYSQL database in C#.NET

查看:98
本文介绍了如何在C#.NET中备份MYSQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想备份MYSQL数据库,我尝试使用以下代码;它无法正常工作

i want to take backup of the MYSQL database i have try the following code it;s not working properly

DateTime backupTime = DateTime.Now;
                   int year = backupTime.Year;
                   int month = backupTime.Month;
                   int day = backupTime.Day;
                   int hour = backupTime.Hour;
                   int minute = backupTime.Minute;
                   int second = backupTime.Second;
                   int ms = backupTime.Millisecond;

                   String tmestr = backupTime.ToString();
                   tmestr = "C:\\" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".sql";
                   StreamWriter file = new StreamWriter(tmestr);
                   ProcessStartInfo proc = new ProcessStartInfo();
                   string cmd = string.Format(@"-u {0} -p {1} -h {2}  {3} >  {4};", "root", "", "localhost", "globasys", "backup.sql");
                   proc.FileName = @"C:\Program Files\MySQL\MySQL Server 6.0\bin\mysqldump";
                   proc.RedirectStandardInput = false;
                   proc.RedirectStandardOutput = true;
                   proc.Arguments = cmd;//"-u root -p smartdb > testdb.sql";
                   proc.UseShellExecute = false;
                   Process p = Process.Start(proc);
                   string res;
                   res = p.StandardOutput.ReadToEnd();
                   file.WriteLine(res);
                   p.WaitForExit();
                   file.Close();


此代码创建. SQL文件,但是在将该文件还原到数据中时,它会显示错误消息.txt文件后的SQL转储错误显示错误消息


this code create the . SQL file but when restore this file in to data it display the error message SQL dump error following txt file display the error message

Query:
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT *//*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */

Error occured at:2011-10-17 15:23:28
Line no.:8
Error Code: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */'' at line 1

Query:
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT *//*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */

Error occured at:2011-10-18 15:33:23
Line no.:8
Error Code: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */'' at line 1

Query:
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT *//*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */

Error occured at:2011-10-18 15:34:56
Line no.:8
Error Code: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */'' at line 1


如何在C#.NET中备份MYSQL数据库


how to take backup of the MYSQL database in C#.NET

推荐答案

使用MySqlDump是正确的任务.看看我在 MySql Backup Utility [
Using MySqlDump is a real mission to get right. Take a look at my article on MySql BackUp Utility[^]. The article is in VB.Net but it is a separate class and can be used in C# apps.

Hope this helps


此解决方案适用于小型数据库.当它运行以获取大数据库(arnd Gb)时失败..
This solution works for small size db. when it runs for taking db of big size ( arnd Gb) it fails..


请尝试以下操作:
在C#/VB中制作自己的MySQL备份和还原工具.NET/ASP.NET-MySqlBackup.DLL(支持Unicode,UTF8) [
Try this:
Making Your Own MySQL Backup and Restore Tools in C#/VB.NET/ASP.NET - MySqlBackup.DLL (Supports Unicode, UTF8)[^]


这篇关于如何在C#.NET中备份MYSQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆