备份和恢复数据库方法

时间:2015/12/3 16:45:00来源:互联网 作者:flyso 点击: 996 次

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;

            }

        }     

Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3