Search code examples
c#.netmysqlwinformsbackup

how to backup the database using mysql and c#.net


I have found this solution for back up my database (mysql) using c#.net

string fname = txtFileName.Text;


if (fname == "")
{

 MessageBox.Show("Please Enter the File Name!");return;
}

try
{

      btnBackup.Enabled = false;
      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();

 // C:\Program Files\MySQL\MySQL Server 5.0\bin

   //tmestr = "C:\\" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".bak";

    tmestr = "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\" + fname + year + "-" + month + "-" + day + "-" + hour;// +".sql";

   StreamWriter file = new StreamWriter(tmestr);
  ProcessStartInfo proc = new ProcessStartInfo();

    string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname);
     proc.FileName = "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();

  MessageBox.Show("DataBase Backup Has Been Completed Successfully!");btnBackup.Enabled = true;
}

catch (IOException ex)
{

  MessageBox.Show("Disk full or other IO error , unable to backup!");
}

txtFileName.Text = "";

which value i have to give in this text box "txtfilename.txt"

and what i have to give in this values @"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname

I have found the mysqldump.exe file in this location

string location = "C:\\Program Files\\MySQL\\MySQL WorkBench 5.2CE\\";

and this is my connectionstring

string connestring = "server=localhost;user=root;database=access";

I am not sure which values i have to give in these places user, passwd1, Data_Source, dbname

would any one pls help on this guys

Many thanks..


Solution

  • First off, the location of mysqldump.exe that you should be using is within the same directory as mysql itself (C:\Program Files\MySQL\MySQL Server 5.5\bin for example), use that and no other copy.

    There is no connection string.

    In the parent directory (ie C:\Program Files\MySQL\MySQL Server 5.5) you'll find the configuration file my.ini where under the [client] heading you can set the connection settings (username/password etc). Of if you prefer, you specify the login information as arguments when starting the mysqldump process (a list of arguments is provided by MySQL).

    An example, will dump everything in the databases you specify (data, structure, triggers, the lot, and will overwrite any tables when you then import it back again, use the command line arguments depending on what you want).

        public static void DumpStructure()
    {
        Process sd = null;
        ProcessStartInfo r1 = new ProcessStartInfo(/* Full path to MySqlDump.exe */, "--databases exampleDatabase1 exampleDatabase2 --compress --routines --triggers --add-drop-database --add-drop-table --add-locks --extended-insert --password=YOURPASSWORD --port=8307 --user=YOURUSERNAME --disable-keys --quick --comments --complete-insert --result-file=DUMPEDOUTPUT.sql");
    
        r1.CreateNoWindow = true;
        r1.WorkingDirectory = /* WHERE MYSQL.EXE IS STORED */;
        r1.UseShellExecute = false;
        r1.WindowStyle = ProcessWindowStyle.Minimized;
        r1.RedirectStandardInput = false;
    
        sd = Process.Start(r1);
        sd.WaitForExit();
    
        if (!sd.HasExited) {
            sd.Close();
        }
        sd.Dispose();
        r1 = null;
        sd = null;  
    }