【发布时间】:2011-12-02 22:18:33
【问题描述】:
我找到了使用 c#.net 备份我的数据库 (mysql) 的解决方案
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 = "";
我必须在这个文本框中给出哪个值 "txtfilename.txt"
我必须在这个值中给出什么@"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname
我在这个位置找到了mysqldump.exe文件
string location = "C:\\Program Files\\MySQL\\MySQL WorkBench 5.2CE\\";
这是我的连接字符串
string connestring = "server=localhost;user=root;database=access";
我不确定在这些地方我必须给出哪些值user, passwd1, Data_Source, dbname
有没有人能帮帮忙
非常感谢..
【问题讨论】:
-
有另一种方法可以做到这一点:阅读这里:codeproject.com/Articles/256466/…
标签: c# .net mysql winforms backup