Tom-Net

备份数据库到本地

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 System.IO;

public partial class DatabaseBackup : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);//建立数据连接

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnBackup_Click(object sender, EventArgs e)
    {
        SqlCommand comm = new SqlCommand("DatabaseBackup",conn);//执行数据库备份
        comm.CommandType = CommandType.StoredProcedure;
        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
            TransportFile();//传送备份文件
            DelOldBackupFile();//删除过期备份文件
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
      
    }

    public void TransportFile()//将备份的文件传送到本机
    {
        System.IO.Stream iStream = null;

        //以10K为单位缓存:
        byte[] buffer = new Byte[10000];

        int length;

        long dataToRead;

        // 制定文件路径.
        string datestr = System.DateTime.Now.Date.ToString("yyyyMMdd");//系统当前日期YYYY-MM-DD
        //string formatdatestr = datestr.Substring(0,4) + datestr.Substring(5,2) + datestr.Substring(8,2);
        string filepath = @"E:\study\DMS\File\"+datestr+"";

        //  得到文件名.
        string filename = System.IO.Path.GetFileName(filepath);

        try
        {
            // 打开文件.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,System.IO.FileAccess.Read, System.IO.FileShare.Read);


            // 得到文件大小:
            dataToRead = iStream.Length;

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename+"BAK");

            while (dataToRead > 0)
            {
                //保证客户端连接
                if (Response.IsClientConnected)
                {
                    length = iStream.Read(buffer, 0, 10000);

                    Response.OutputStream.Write(buffer, 0, length);

                    Response.Flush();

                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //结束循环
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            // 出错.
            throw ex;
        }
        finally
        {
            if (iStream != null)
            {
                //关闭文件
                iStream.Close();
            }
        }
    }

    public void DelOldBackupFile()//删除过期的备份文件
    {
        string datestrNow = System.DateTime.Now.Date.ToString();//系统当前日期YYYY-MM-DD
        DateTime dt = DateTime.Parse(datestrNow);//转换为Datetime
        DateTime dateadd = dt.AddDays(-30);//当前日期减去30天
        string datestr = dateadd.ToString("yyyyMMdd");//转换为字符串
        string FileName = "~//File//" + datestr;
        if (File.Exists(Server.MapPath(FileName)))
        {
            System.IO.File.Delete(Server.MapPath(FileName));//如果存在30天备份责删除
        }
        else
        {
            return;
        }
    }

}

分类:

技术点:

相关文章: