CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Posts
    8

    Back up mysql database using c#.

    Hi,

    I want to back up mysql database using c sharp and I found following code on google.

    But it is not giving me whole database back up,instead it show following output.


    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    /*!40103 SET TIME_ZONE='+00:00' */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

    /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;


    C# code as below


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Diagnostics;

    namespace CRMService4ConsoleApplication
    {
    class Program
    {
    static void Main(string[] args)
    {
    Program.DatabaseBackup("G:/xampp/mysql/bin/mysqldump.exe", "dbname");
    }

    public static void DatabaseBackup(string ExeLocation, string DBName)
    {
    try
    {
    string tmestr = "";
    tmestr = DBName + "-" + DateTime.Now.ToShortDateString() + ".sql";
    tmestr = tmestr.Replace("/", "-");
    tmestr = "G:/" + tmestr;
    StreamWriter file = new StreamWriter(tmestr);
    ProcessStartInfo proc = new ProcessStartInfo();
    string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "password", "localhost", DBName);
    //string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "password", "localhost", "dbfile");
    proc.FileName = ExeLocation;
    proc.RedirectStandardInput = false;
    proc.RedirectStandardOutput = true;
    proc.Arguments = cmd;
    proc.UseShellExecute = false;
    Process p = Process.Start(proc);
    string res;
    res = p.StandardOutput.ReadToEnd();
    file.WriteLine(res);
    p.WaitForExit();
    file.Close();
    //MessageBox.Show("Backup Completed");
    }
    catch (IOException ex)
    {
    //MessageBox.Show(ex.Message.ToString());
    }
    }
    }
    }

    - Thanks.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Back up mysql database using c#.

    Well, based on the output and the code you posted, I would say that your are supplying the wrong database name to back up.

    Program.DatabaseBackup("G:/xampp/mysql/bin/mysqldump.exe", "dbname");

    Is dbname really the name of the database you want to backup or did you change that name when posting your code?

  3. #3
    Join Date
    Nov 2011
    Posts
    8

    Re: Back up mysql database using c#.

    Hi sotoasty,


    I have change hostname,dbusername,dbpassword and dbname in above code post.

    - Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured