asp.net(C#) 数据库备份还原 源码

Backup.aspx

 

  protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("") + @"/Backup";
        if (!Directory.Exists(path))
        {          
            Directory.CreateDirectory(path);
        }

        try
        {
            path = Convert.ToString(Hst.DBUtility.DbHelperSQL.GetSingle("p_DataBaseBack"));
            LabelMessage.Text = "备份成功; 文件存放路径在:" + path;
        }
        catch
        {
            LabelMessage.Text = "备份失败!请联系管理员";
        }
       
    }

 

DataBack.aspx

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Hst.DBUtility;

public partial class SysManager_DataBack : System.Web.UI.Page
{
    const string FilePath = "E://Backup//";
    int a = 1;
    Hst.SysManage.FileControl fc = new Hst.SysManage.FileControl();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Acurity.AcurityCheck.CheckPower(308, Page, 1);
          
        }
        Bind();
    }
    void Bind()
    {
        Hst.SysManage.FileControl fc = new Hst.SysManage.FileControl();
        DataList1.DataSource = fc.GetFileList(FilePath, "bak");
        DataList1.DataKeyField = "fileName";
        DataList1.DataBind();
    }
    protected void Submit1_ServerClick(object sender, EventArgs e)
    {
        string name = Request.Form["radiobutton"];
        if (name != null)
        {
            string backupstr = "use master exec killspid Data_Hst restore  DATABASE Data_Hst  from disk='" + FilePath.Trim() + name.Trim() + "' with replace;";
            bool result = fc.ExecuteSql(backupstr);
            if (result)
            {
                Hst.Common.MessageBox.Show(Page, "恢复成功!");
            }
            else
            {
                Hst.Common.MessageBox.Show(Page, "恢复失败!请联系管理员!");
            }
        }
        else
        {
            Hst.Common.MessageBox.Show(Page, "请选择一个文件!");

        }
    }
    protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
    {
        a = e.NewPageIndex;
        AspNetPager1.CurrentPageIndex = a;
        Bind();

    }
}

 

FileControl.cs

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace  Hst.SysManage
{
   public  class FileControl
    {
        /// <summary>
        /// 获取指定文件夹内文件列表
        /// </summary>
        /// <param name="FolderPath">父级文件夹路径</param>
        /// <param name="FileType">指定要查找的子文件的文件类型</param>
        /// <returns>返回文件信息表</returns>
        public DataTable GetFileList(string FolderPath, string FileType)
        {
            DirectoryInfo Dic;

            Dic = new DirectoryInfo(FolderPath);//根据父文件夹路径检索子文件夹或文件。

            FileInfo[] dirs = Dic.GetFiles();//将子文件路径存入数组 
            DataTable lic = new DataTable();
            //添加列标题
            lic.Columns.Add("fileName");
            lic.Columns.Add("fileSize");
            lic.Columns.Add("BackTime");
            DataRow li;
            if (dirs.Length > 0)
            {
                foreach (FileInfo file in dirs)
                {

                    if (file.Name.Split(new char[] { '.' }).Length > 1 && file.Name.Split(new char[] { '.' })[1].ToLower() == FileType)
                    {
                        li = lic.NewRow();
                        li["fileName"] = file.Name;
                        li["fileSize"] = CalculateSize(file.Length);
                        li["BackTime"] = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                        lic.Rows.Add(li);
                    }
                }
            }
            return lic;
        }
        /// <summary>
        /// 文件大小格式转换
        /// </summary>
        /// <param name="size">文件大小</param>
        /// <returns>返回规范后的格式</returns>
        private string CalculateSize(long size)
        {
            string length = string.Empty;

            if (size < 1024)
            {
                length = size + "bytes";
            }
            else
                if (size < 1024 * 1024)
                {
                    length = float.Parse((size * 10 / 1024).ToString()) / 10 + "KB";
                }
                else
                    if (size < 1024 * 1024 * 1024)
                    {
                        length = float.Parse((size * 10 / 1048576).ToString()) / 10 + "MB";
                    }
                    else
                    {
                        length = float.Parse((size * 10 / 1073741824).ToString()) / 10 + "GB";
                    }

            return length;
        }
     /// <summary>
     /// creatime:2010-3-1 11
     /// </summary>
     /// <param name="SQLString">sql语句</param>
     /// <returns>返回true 执行成功 </returns>
       public  bool ExecuteSql(string SQLString)
       {
           string connectionString = Hst.DBUtility.PubConstant.ConnectionString;
           using (SqlConnection connection = new SqlConnection(connectionString))
           {
               using (SqlCommand cmd = new SqlCommand(SQLString, connection))
               {
                   try
                   {
                       connection.Open();
                       cmd.ExecuteNonQuery();
                       return true;
                   }
                   catch (System.Data.SqlClient.SqlException e)
                   {
                       return false;
                       connection.Close();
                       throw e;
                   }

               }
           }
       }
    }
}

 

存储 过程

 

USE [master]
GO
/****** Object:  StoredProcedure [dbo].[killspid]    Script Date: 05/19/2010 12:57:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create   proc     [dbo].[killspid]     (@dbname     varchar(20))      
  as      
  begin      
  declare     @sql     nvarchar(500)      
  declare     @spid     int      
  set     @sql='declare     getspid     cursor     for          
  select     spid     from     sysprocesses     where     dbid=db_id('''+@dbname+''')'      
  exec     (@sql)      
  open     getspid      
  fetch     next     from     getspid     into     @spid      
  while     @@fetch_status     <     >-1      
  begin      
  exec('kill     '+@spid)      
  fetch     next     from     getspid     into     @spid      
  end      
  close     getspid      
  deallocate     getspid      
  end 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值