• 备份恢复数据库


    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;

                }

            }

  • 相关阅读:
    【分享】IT产业中的三大定理(二) —— 安迪&比尔定理 (Andy and Bill's Law)
    【分享】IT产业中的三大定理(一) —— 摩尔定理(Moore's Law)
    Linux(Ubuntu)下也能用搜狗输入法了!!!
    聊聊付费软件
    JSP中使用Spring注入的Bean时需要注意的地方
    Spark (十二) Spark Streaming详解
    Kafka(一) 初识
    Spark (十一) spark使用hive的元数据信息
    Spark (十) Spark 的种类型Join
    Spark 学习(九) SparkSQL 函数自定义和数据源
  • 原文地址:https://www.cnblogs.com/jcomet/p/1933924.html
Copyright © 2020-2023  润新知