当前位置:  编程技术>.net/c#/asp.net

ASP.NET实现在线备份与恢复Sql server

    来源: 互联网  发布时间:2014-08-30

    本文导语:          注:最近做的一个项目因为服务器是在特殊机房上的,因为安全方面的考虑,不能给我们开发者提供FTP服务,所以每次更新版本都得自己跑一趟,而他的机房有很远,所以我一直想能不能开发一个维护版本的系统呢...

        注:最近做的一个项目因为服务器是在特殊机房上的,因为安全方面的考虑,不能给我们开发者提供FTP服务,所以每次更新版本都得自己跑一趟,而他的机房有很远,所以我一直想能不能开发一个维护版本的系统呢,对数据库和代码进行在线更新,就不用自己跑了,于是就有了下面的尝试,在线恢复和备份SQL Server:

前台代码:
 

代码如下:




   


   
   

       
           
               
                    操 作 数 据 库
               
               
                   
                   
                   
               
               
               
           
           
               
                    备份名称和位置
               
               
                   
               
               
                    (如D:beifen)
               
           
           
               
                   
               
           
       
   

   

       
           
               
                    操 作 数 据 库
               
               
                   
                   
               
               
               
           
           
               
                    操 作 数 据 库
               
               
                   
               
               
               
           
           
               
                   
                   
               
           
       
   

   

后台代码:
 

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Diagnostics;

namespace SysSourceMgmt
{
    public partial class SqlDbMgmt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd=";
                    string SqlStr2 = "Exec sp_helpdb";
                    SqlConnection con = new SqlConnection(SqlStr1);
                    con.Open();
                    SqlCommand com = new SqlCommand(SqlStr2, con);
                    SqlDataReader dr = com.ExecuteReader();
                    this.DropDownList1.DataSource = dr;
                    this.DropDownList1.DataTextField = "name";
                    this.DropDownList1.DataBind();
                    dr.Close();
                    con.Close();

                    SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd=";
                    SqlStr2 = "Exec sp_helpdb";
                    con = new SqlConnection(SqlStr1);
                    con.Open();
                    com = new SqlCommand(SqlStr2, con);
                    dr = com.ExecuteReader();
                    this.DropDownList1.DataSource = dr;
                    this.DropDownList1.DataTextField = "name";
                    this.DropDownList1.DataBind();
                    dr.Close();
                    con.Close();
                }
                catch (Exception)
                {
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string dbName = string.Empty;
            if (DropDownList1.Items.Count != 0)
            {
                dbName = DropDownList1.SelectedValue.Trim();
            }
            else
            {
                dbName = txtDbName.Text.Trim();
            }

            string SqlStr1 = "Data Source=.\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True";
            string SqlStr2 = "backup database " + dbName + " to disk='" + this.TextBox1.Text.Trim() + ".bak'";
            SqlConnection con = new SqlConnection(SqlStr1);
            con.Open();
            try
            {
                if (File.Exists(this.TextBox1.Text.Trim()))
                {
                    Response.Write("alert('此文件已存在,请从新输入!');location='Default.aspx'");
                    return;
                }
                SqlCommand com = new SqlCommand(SqlStr2, con);
                com.ExecuteNonQuery();
                Response.Write("alert('备份数据成功!');'");
            }
            catch (Exception error)
            {
                Response.Write(error.Message);
                Response.Write("alert('备份数据失败!')");
            }
            finally
            {
                con.Close();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称
            string dbName = string.Empty;
            if (DropDownList1.Items.Count != 0)
            {
                dbName = DropDownList1.SelectedValue.Trim();
            }
            else
            {
                dbName = txtDbName.Text.Trim();
            }

            string SqlStr1 = "Data Source=.\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True";
            string SqlStr2 = @"use master restore database " + dbName + " from disk='" + path + "'";
            SqlConnection con = new SqlConnection(SqlStr1);
            con.Open();
            try
            {
                SqlCommand com = new SqlCommand(SqlStr2, con);
                com.ExecuteNonQuery();
                Response.Write("alert('还原数据成功!');'");
            }
            catch (Exception error)
            {
                Response.Write(error.Message);
                Response.Write("alert('还原数据失败!')");

                txtDbName.Text = SqlStr2;
            }
            finally
            {
                con.Close();
            }
        }

        ///
        /// 恢复数据库,可选择是否可以强制还原(即在其他人在用的时候,依然可以还原)
        ///
        /// 待还原的数据库名称
        /// 带还原的备份文件的完全路径
        /// 恢复数据库失败的信息
        /// 是否强制还原(恢复),如果为TRUE,则exec killspid '数据库名' 结束此数据库的进程,这样才能还原数据库
        ///
        public bool RestoreDataBase(string databasename, string databasefile, ref string returnMessage, bool forceRestore, SqlConnection conn)
        {
            bool success = true;
            string path = databasefile;
            string dbname = databasename;

            string restoreSql = "use master;";

            if (forceRestore)//如果强制回复
                restoreSql += string.Format("use master exec killspid '{0}';", databasename);

            restoreSql += "restore database @dbname from disk = @path;";

            SqlCommand myCommand = new SqlCommand(restoreSql, conn);

            myCommand.Parameters.Add("@dbname", SqlDbType.Char);
            myCommand.Parameters["@dbname"].Value = dbname;
            myCommand.Parameters.Add("@path", SqlDbType.Char);
            myCommand.Parameters["@path"].Value = path;

            Response.Write(restoreSql);
            try
            {
                myCommand.Connection.Open();
                myCommand.ExecuteNonQuery();
                returnMessage = "还原成功";
            }
            catch (Exception ex)
            {
                returnMessage = ex.Message;
                success = false;
            }
            finally
            {
                myCommand.Connection.Close();
            }

            return success;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称
            string dbName = string.Empty;
            if (DropDownList1.Items.Count != 0)
            {
                dbName = DropDownList1.SelectedValue.Trim();
            }
            else
            {
                dbName = txtDbName.Text.Trim();
            }
            string returnMessage = string.Empty;
            string SqlStr1 = "Data Source=.\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True";
            SqlConnection con = new SqlConnection(SqlStr1);
            RestoreDataBase(txtDbName.Text, path, ref returnMessage, true,con);

            Response.Write(returnMessage);
        }
    }
}

效果图:

经过试验,大体完成了我需要的功能,具体优化后期进行中。


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 在ASP中可以轻易实现,不知道在JSP中能否实现?
  • Linux平台下哪种方法实现ASP好?
  • 求解!Java如何能在多个applet之间实现类似asp的session类!
  • 调试jsp的时如何实现像asp中的response.end的效果
  • 怎么用能让asp实现统计在线人数的功能。在线等待!!!帮忙送分!
  • 在jsp 中如何实现像asp 中的页面弹出式的菜单
  • asp.net 参数不同共用一个页面的实现方法
  • Asp.net防止重复提交的实现方法
  • 用linux做服务器运行ASP.net网站,请问具体怎样实现??
  • ASP.NET MVC3 实现全站重定向的简单方法
  • 在jsp中如何实现与asp中的Request.ServerVariables("SCRIPT_NAME")同等的功能?
  • Jsp中怎么实现类似ASP中split函数的功能?
  • 如何实现ASP中类似Global.asa的功能???
  • jsp中如何实现asp中的response.end,或php中的exit()功能?
  • java实现的asp服务器 OpenASP
  • ASP的功能在JSP中怎么实现呀?谢谢!!!
  • asp去掉html,保留img br p div的正则实现代码
  • asp.net 实现php的md5()函数功能
  • asp.net session实现用户登录的疑问
  • 怎样将Tomcat嵌入IIS中,实现用ASP与JSP都用IIS来做!
  • ASP.NET之 Ajax相关知识介绍及组件图
  • 我想了解一些关于Java怎样与Asp或Asp.net结合方面在未来发展方向的问题?
  • c#/ASP.NET操作cookie(读写)代码示例
  • asp.net UrlEncode对应asp urlencode的处理方法
  • asp.net实例 定义和使用asp:AccessDataSource
  • win2008 r2 服务器环境配置(FTP/ASP/ASP.Net/PHP)
  • asp与asp.net的session共享
  • 如何在unix下发布asp?
  • 怎么让Apache支持Asp?
  • ??谁能把ASP代码改为JSP的
  • ASP和ASP.Net共享Session解决办法


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3