Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

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

how to take backup of the MYSQL database in C#.NET
Posted
Updated 18-Oct-11 0:17am
v3

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
 
Share this answer
 
Comments
vrushali katkade 20-Oct-11 7:46am    
in your article code MYSQL.data.dll file can not found & when add this file to your code it will not get the reference of this file .can you please tell me the version of the MYSQL dll file because i have 5.0.9 version
Wayne Gaylard 20-Oct-11 8:31am    
Have you installed the MySql Connector for .Net ? If you have, then you just have to remove the reference to MySql.Data in my project and then add a reference to your installed version. Then you will have to compile the project again, and it should all work. Sorry that I cannot remember the version I used for that project, and it is not on the machine I am using at the moment.
This solution works for small size db. when it runs for taking db of big size ( arnd Gb) it fails..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900