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;

                }

            }

  • 相关阅读:
    关于重载和重写的区别
    UML的关联(Association), 聚合(Aggregation), 组合(Composition)区别
    解析CSS加密技术之“障眼法”
    ASP.NET中MEMCACHED
    新建项目的无法应用已有项目
    C# 判断两张图片是否一致的快速方法
    压力测试中需要掌握的几个基本概念
    软件测试Web数据分析工具HttpWatch安装
    在SQL Server实现最短路径的搜索
    从算法入手讲解如何在SQL Server中实现最优最简
  • 原文地址:https://www.cnblogs.com/jcomet/p/1933924.html
Copyright © 2011-2022 走看看