zoukankan      html  css  js  c++  java
  • 备份恢复数据库

    protected void btnhuifu_Click(object sender, EventArgs e)

            {

                //string path = Page.MapPath("../App_Data").ToString();

                //if (new Maticsoft.BLL.Setting().HuiFu(path))

                //{

                //    this.Label1.Text = "数据恢复成功";

                //}

                //else

                //{

                //    this.Label1.Text = "数据恢复失败,数据库正在使用";

                //}

                string StrPath = Server.MapPath("../App_Data");

                if (FileUpbak.HasFile)

                {

                    string filename = StrPath + "/" + FileUpbak.FileName;

                    FileUpbak.SaveAs(filename);

                    if (new Maticsoft.BLL.Setting().HuiFu(filename))

                    {

                        this.Label1.Text = "数据库恢复成功";

                    }

                    else

                    {

                        this.Label1.Text = "数据库恢复未成功";

                    }

                }

                this.Label1.Text = "请选择要恢复的数据文件";

            }

            protected void btnbeifei_Click(object sender, EventArgs e)

            {

                string StrPath = Server.MapPath("../App_Data");

                string filename = DateTime.Now.ToString("yyyyMMddHHMMss");

                if (new Maticsoft.BLL.Setting().BeiFen(StrPath + "\\" + filename))

                {

                    string filePath = StrPath + "\\" + filename;//路径

                    //以字符流的形式下载文件 

                    FileStream fs = new FileStream(filePath, FileMode.Open);

                    byte[] bytes = new byte[(int)fs.Length];

                    fs.Read(bytes, 0, bytes.Length);

                    fs.Close();

                    Response.ContentType = "application/octet-stream";

                    //通知浏览器下载文件而不是打开 

                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));

                    Response.BinaryWrite(bytes);

                    Response.Flush();

                    Response.End();

                    File.Delete(StrPath + "\\" + filename);

                }

                //string path = Page.MapPath("../App_Data/").ToString();

                //if (new Maticsoft.BLL.Setting().BeiFen(path+DateTime.Now.ToString("yyyymmddhhmmss")+".bak"))

                //{

                //    this.Label1.Text = "备份成功";

                //}

                //else

                //{

                //    this.Label1.Text = "备份失败";

                //}

            }

    public bool BeiFen(string path)

            {

                try

                {

                    string backupstr = "backup database EmailDataBase to disk='" + path + "';";

                    SqlHelper.ExecuteNonQuery(CommandType.Text, backupstr, null);

                    return true;

                }

                catch

                {

                    return false;

                }

            }

            public bool HuiFu(string path)

            {

                try

                {

                    //alter database EmailDataBase set offline with rollback immediate ;

                    string restore = "use master ;ALTER DATABASE [EmailDataBase] SET OFFLINE WITH ROLLBACK IMMEDIATE;restore database EmailDataBase from disk='" + path + "' with replace;";

                    SqlHelper.ExecuteNonQuery(CommandType.Text, restore, null);

                    return true;

                }

                catch

                {

                    return false;

                }

            }

  • 相关阅读:
    《C# to IL》第一章 IL入门
    multiple users to one ec2 instance setup
    Route53 health check与 Cloudwatch alarm 没法绑定
    rsync aws ec2 pem
    通过jvm 查看死锁
    wait, notify 使用清晰讲解
    for aws associate exam
    docker 容器不能联网
    本地运行aws lambda credential 配置 (missing credential config error)
    Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
  • 原文地址:https://www.cnblogs.com/jcomet/p/1933924.html
Copyright © 2011-2022 走看看