C#用mysqldump备份mysql还原mysql数据库,急!!在线等!!!
C#用mysqldump备份mysql还原mysql数据库,急!!在线等!!! --------------------编程问答-------------------- --------------------编程问答-------------------- csdn怎么人越来越少了? --------------------编程问答-------------------- public static void Backup(){
string registerLocation = @"Software\MySQL AB\MySQL Server 5.0";
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(registerLocation, true);
if (key != null)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "数据备份脚步.sql|(*.sql)";
save.FileName = (DateTime.Now.ToString("yyyyMMddhhmmss") + ".sql");
save.ShowDialog();
if (save.FileName != null)
{
string location = key.GetValue("Location").ToString();
ProcessStartInfo psi = new ProcessStartInfo(location + @"bin\mysqldump.exe");
psi.Arguments = @"--default-character-set utf8 -u root --password=123 --database dt_ceshi > backup.sql";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
Process pro = Process.Start(psi);
Stream baseStream = pro.StandardOutput.BaseStream;
StreamReader sr = new StreamReader(baseStream, System.Text.Encoding.UTF8);
string strSqldump = sr.ReadToEnd();
FileStream fs = File.Create(save.FileName);
byte[] arr = UnicodeEncoding.UTF8.GetBytes(strSqldump);
fs.Write(arr, 0, arr.Length);
fs.Flush();
fs.Close();
pro.Close();
}
}
}
public static void Restore()
{
string registerLocation = @"Software\MySQL AB\MySQL Server 5.0";
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(registerLocation, true);
if (key != null)
{
OpenFileDialog open = new OpenFileDialog();
open.ShowDialog();
if (open.FileName != null)
{
string location = key.GetValue("Location").ToString();
string aa = "mysql -hlocalhost -uroot -p123 dt_ceshi<\""+open.FileName+"\"";
Process p = new Process();
p.StartInfo.FileName = @"C:\WINDOWS\system32\cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("C:");
p.StandardInput.WriteLine("CHDIR \"" + location + "bin\"");
p.StandardInput.WriteLine(aa);
p.StandardInput.WriteLine("exit");
MessageBox.Show(p.StandardOutput.ReadToEnd());
p.Close();
}
//
}
}
补充:.NET技术 , C#