ASP.NET SQL SERVER 2000 备份数据库,并传送备份文件

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.IO;
None.gif
None.gif
public   partial   class  DatabaseBackup : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    SqlConnection conn 
= new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);//建立数据连接
InBlock.gif

InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void BtnBackup_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlCommand comm 
= new SqlCommand("DatabaseBackup",conn);//执行数据库备份
InBlock.gif
        comm.CommandType = CommandType.StoredProcedure;
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Open();
InBlock.gif            comm.ExecuteNonQuery();
InBlock.gif            TransportFile();
//传送备份文件
InBlock.gif
            DelOldBackupFile();//删除过期备份文件
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
catch (SqlException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw ex;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif       
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void TransportFile()//将备份的文件传送到本机
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        System.IO.Stream iStream 
= null;
InBlock.gif
InBlock.gif        
//以10K为单位缓存:
InBlock.gif
        byte[] buffer = new Byte[10000];
InBlock.gif
InBlock.gif        
int length;
InBlock.gif
InBlock.gif        
long dataToRead;
InBlock.gif
InBlock.gif        
// 制定文件路径.
InBlock.gif
        string datestr = System.DateTime.Now.Date.ToString("yyyyMMdd");//系统当前日期YYYY-MM-DD
InBlock.gif        
//string formatdatestr = datestr.Substring(0,4) + datestr.Substring(5,2) + datestr.Substring(8,2);
InBlock.gif
        string filepath = @"E:\study\DMS\File\"+datestr+"";
InBlock.gif
InBlock.gif        
//  得到文件名.
InBlock.gif
        string filename = System.IO.Path.GetFileName(filepath);
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 打开文件.
InBlock.gif
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,System.IO.FileAccess.Read, System.IO.FileShare.Read);
InBlock.gif
InBlock.gif
InBlock.gif            
// 得到文件大小:
InBlock.gif
            dataToRead = iStream.Length;
InBlock.gif
InBlock.gif            Response.ContentType 
= "application/octet-stream";
InBlock.gif            Response.AddHeader(
"Content-Disposition""attachment; filename=" + filename+"BAK");
InBlock.gif
InBlock.gif            
while (dataToRead > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//保证客户端连接
InBlock.gif
                if (Response.IsClientConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    length 
= iStream.Read(buffer, 010000);
InBlock.gif
InBlock.gif                    Response.OutputStream.Write(buffer, 
0, length);
InBlock.gif
InBlock.gif                    Response.Flush();
InBlock.gif
InBlock.gif                    buffer 
= new Byte[10000];
InBlock.gif                    dataToRead 
= dataToRead - length;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//结束循环
InBlock.gif
                    dataToRead = -1;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 出错.
InBlock.gif
            throw ex;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (iStream != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//关闭文件
InBlock.gif
                iStream.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

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

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

如下是备份数据库应储存过程

CREATE PROCEDURE [dbo].[DatabaseBackup] AS
declare @DBName varchar(10)--当前数据库名称
declare  @FileName varchar(100)--取当前的日期为保存文件名
declare   @FilePath  varchar(100)--保存文件路径
--declare  @DelFileName varchar(100)--删除当前日期前30天文件
declare  @DelFilePath   varchar(100)--删除文件路径
set @DBName=(select   db_name()   as   databasename)--查询当前数据库名称
set @FileName=(convert(varchar(100),getdate(),112))--当前日期为文件名
set  @FilePath= 'E:\study\DMS\File\'+@FileName
--set  @DelFileName=(convert(varchar(100),dateadd(day,-30,getdate()),112))--取得文件名
--set   @DelFilePath ='E:\study\DMS\File\'+@DelFileName--删除文件路径
backup database @DBName  to disk =@FilePath--保存数据库备份文件到相应路径
GO

对于此存储过程,如果重复执行在一个备份文档中会包含几个不同版本的备份文件,在恢复的时候需要选择时间点.

另传送备份文件到客户端,采用转换为Stream,避免了传送大文件造成的时间长甚至停止响应的情况。

对于恢复操作,可以选择本机备份的文件上传服务器,在恢复服务器数据库之前先备份系统当前数据,具体解决步骤差不多,有时间测试一下。

望各位给出自己的解决方案,是否还有更有效的解决办法。

转载于:https://www.cnblogs.com/mygood/articles/bfmssql2000.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值