larry cheah

larry cheah

  • NA
  • 5
  • 0

Need help on C# windows application mysql backup

Dec 2 2008 9:58 PM
I get this source code from one of the site, this program only work when the *.exe is located inside c:\program files\mysql\mysql server 5.0\bin. May i know how to change the setting so it works at any location?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Demo_System
{
    public partial class frmBackupDB : Form
    {
        public frmBackupDB()
        {
            InitializeComponent();
        }

        private void frmBackupDB_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                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}", "root", "1234", "localhost", "testdb");
                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();

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

Answers (2)