asp.net备份还原mysql数据库 功能已经测试成功

来源:互联网 发布:淘宝pc端首页装修 编辑:程序博客网 时间:2024/05/22 10:25

 

 protected void Page_Load(object sender, EventArgs e)    {        UILogic.ClearCache();        VPUserInfo userinfo = UILogic.getSession() as VPUserInfo;        if (userinfo == null)        {            //Response.Write("<mce:script type="text/javascript"><!--top.location.href='~/../login.aspx'// --></mce:script>");        }        else        {            //if (!Page.IsPostBack)            //{                System.IO.Directory.CreateDirectory("C://Program Files//MySQL//MySQL Server 5.0//data//vpdata");//在服务器mysql中创建vpdata数据库            //}        }    }    protected void Backup_Click(object sender, ImageClickEventArgs e)    {        try        {            //string filename = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "");            string filePath = "D://web//App_Data";            string fileName = filePath + "//vpdata.sql";//备份的文件名称和路径            //判断目录是否存在            if (!System.IO.File.Exists(fileName))            {                System.IO.Directory.CreateDirectory(filePath);            }            //构建执行的命令            String command = string.Format("mysqldump --quick --host=localhost --default-character-set=latin1 --lock-all-tables --port=3306 --user=root --password=123456 --databases vpdata -R >D://web//App_Data//{0}.sql", "vpdata");            //获取mysqldump.exe所在路径            String appDirecroty = @"C:/Program Files/MySQL/MySQL Server 5.0/bin/";            StartCmd(appDirecroty, command);            ScriptManager.RegisterClientScriptBlock(Backup, GetType(), "yes", "alert('数据库已成功备份到D://web//App_Data//Vpdata文件中')", true);        }        catch (Exception ex)        {            ScriptManager.RegisterClientScriptBlock(Backup, GetType(), "no", "alert('数据库备份失败!')", true);        }    }    protected void Restore_Click(object sender, ImageClickEventArgs e)    {        try        {            //构建执行的命令            String command = string.Format("mysql --host=192.168.1.10 --default-character-set=latin1 --port=3306 --user=root --password=123456 vpdata <D://web//App_Data//{0}.sql", "vpdata");            //获取mysql.exe所在路径            String appDirecroty = @"C:/Program Files/MySQL/MySQL Server 5.0/bin/";            StartCmd(appDirecroty, command);            ScriptManager.RegisterClientScriptBlock(Restore, GetType(), "yes", "alert('服务器数据库还原成功!')", true);        }        catch (Exception ex)        {            ScriptManager.RegisterClientScriptBlock(Restore, GetType(), "no", "alert('数据库还原失败!')", true);        }    }    /// <summary>    /// 执行Cmd命令    /// </summary>    /// <param name="workingDirectory">要启动的进程的目录</param>    /// <param name="command">要执行的命令</param>    public static void StartCmd(String workingDirectory, String command)    {        Process p = new Process();        p.StartInfo.FileName = "cmd.exe";        p.StartInfo.WorkingDirectory = workingDirectory;        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(command);        p.StandardInput.WriteLine("exit");    }