/// <summary>
/// 备份数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void t_tsmi_BackupDatabase_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "数据备份脚本.sql|(*.sql)";
saveFileDialog.FileName = (DateTime.Now.ToString("yyyyMMddhhmmss") + ".sql");
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog.FileName != null)
{
string location = Application.StartupPath.ToString();
ProcessStartInfo psi = new ProcessStartInfo(location + @"\mysqldump.exe");
psi.Arguments = @"--default-character-set utf8 -u root --password=sa --database logclient31 > backup.sql";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = 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(saveFileDialog.FileName);
byte[] arr = UnicodeEncoding.UTF8.GetBytes(strSqldump);
fs.Write(arr, 0, arr.Length);
fs.Flush();
fs.Close();
pro.Close();
//数据库已成功备份
}
}
}
catch (Exception ex)
{
//数据库备份失败 }
}
其中备份出来的脚本文件中没有 create function 语句。