Backup:
DateTime Time = DateTime.Now;int year = Time.Year;
int month = Time.Month;
int day = Time.Day;
int hour = Time.Hour;
int min = Time.Minute;
//int second = Time.Second;
//int millisecond = Time.Millisecond;
//Save file to C:\ with the current date as a filename
string path = Application.StartupPath + "\\"+ "dbbackup"+"\\";
path = path + year + "-" + month + "-" + day + "-" + hour + "-" + min + ".sql";
//path = "C:\\Documents and Settings\\user\\Desktop\\dbbackup\\" + year + "-" + month + "-" + day +".sql";
StreamWriter file = new StreamWriter(path);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "mysqldump";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "password", "localhost", "inventory");
psi.UseShellExecute = false;
Process process = Process.Start(psi);
string output;
output = process.StandardOutput.ReadToEnd();
file.WriteLine(output);
process.WaitForExit();
file.Close();
process.Close();
MessageBox.Show("Database Backup Completed Successfully");
Restore:
int x = 10; int y = 10;
files = Directory.GetFiles(Application.StartupPath + "\\" + "dbbackup", "*.sql");
for (int i = 0; i < files.Length; i++)
{
split = files[i].Split('\\');
foreach (string file in split)
{
if (file.EndsWith(".sql"))
{
LinkLabel lb = new LinkLabel();
lb.Location = new Point(x, y);
lb.Name = file;
lb.Text = file;
lb.Width = 300;
dbfilepanel.Controls.Add(lb);
y = y + 20;
lb.Click += new EventHandler(lb_Click);
in lb_Click Event:
string dbfile = ((Control)sender).Name;
string respath = Application.StartupPath + "\\" + "dbbackup" + "\\" + dbfile;
/* for restoring the database */
StreamReader file = new StreamReader(respath);
ProcessStartInfo proc = new ProcessStartInfo();
string cmdArgs = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "password", "localhost", "inventory");
proc.FileName = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe";
proc.RedirectStandardInput = true;
proc.RedirectStandardOutput = false;
proc.Arguments = cmdArgs;
proc.UseShellExecute = false;
Process p = Process.Start(proc);
string res = file.ReadToEnd();
file.Close();
p.StandardInput.WriteLine(res);
p.Close();
MessageBox.Show("Database Restore Completed Successfully");
No comments:
Post a Comment