asp.net|备份|恢复|数据|数据库|页面using System;
using SQLDMO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace bakServe040907
{
/// <summary>
/// 数据库备份类说明
/// </summary>
public class bakServer
{
string ServerName; //数据服务器名称
string UserName; //用户名称
string Password; //用户密码
string message; //消息提示
ProgressBar PBar; //状态条控件
public bakServer()
{
}
/// <summary>
/// 取得数据库服务器列表
/// </summary>
/// <returns>数据库服务器列表</returns>
public ArrayList GetServerList()
{
ArrayList alServers = new ArrayList() ;
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass() ;
try
{
SQLDMO.NameList serverList = sqlApp.ListAvailableSQLServers() ;
for(int i = 1;i<= serverList.Count;i++)
{
alServers.Add(serverList.Item(i)) ;
}
}
catch(Exception e)
{
// throw(new Exception("取数据库服务器列表出错:"+e.Message)) ;
message = "取数据库服务器列表出错:" +e.Message;
}
finally
{
sqlApp.Quit() ;
}
return alServers ;
}
/// <summary>
/// 错误消息处理
/// </summary>
/// <returns>消息信息</returns>
public string Msg()
{
return message;
}
/// <summary>
/// 取得指定数据库列表
/// </summary>
/// <param name="strServerName">服务器名称</param>
/// <param name="strUserName">用户名称</param>
/// <param name="strPwd">用户密码</param>
/// <returns>数据库列表</returns>
public ArrayList GetDbList(string strServerName,string strUserName,string strPwd)
{
ServerName = strServerName ;
UserName = strUserName ;
Password = strPwd ;
ArrayList alDbs = new ArrayList() ;
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass() ;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
svr.Connect(ServerName,UserName,Password) ;
foreach(SQLDMO.Database db in svr.Databases)
{
if(db.Name!=null)
alDbs.Add(db.Name) ;
}
}
catch(Exception err)
{
// throw(new Exception("连接数据库出错:"+e.Message)) ;
message = "连接数据库出错:" +err.Message;
}
finally
{
svr.DisConnect() ;
sqlApp.Quit() ;
}
return alDbs ;
}
//数据库的备份和实时进度显示代码:
/// <summary>
/// 数据库的备份和实时进度显示
/// </summary>
/// <param name="strDbName">数据库名称</param>
/// <param name="strFileName">备份文件名</param>
/// <param name="pgbMain">状态条控件名称</param>
/// <param name="strServerName">服务器名称</param>
/// <param name="strUserName">用户名称</param>
/// <param name="strPwd">密码</param>
/// <returns>备份成功返回true ,否则返回false</returns>
public bool BackUPDB(string strDbName,string strFileName, ProgressBar pgbMain,string strServerName,string strUserName,string strPwd)
{
ServerName = strServerName ;
UserName = strUserName ;
Password = strPwd ;
PBar = pgbMain;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
svr.Connect(ServerName,UserName,Password) ;
SQLDMO.Backup bak = new SQLDMO.BackupClass();
bak.Action = 0 ;
bak.Initialize = true ;
SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step);
bak.PercentComplete += pceh;
bak.Files = strFileName;
bak.Database = strDbName;
bak.SQLBackup(svr);
return true ;
}
catch(Exception err)
{
// throw(new Exception("备份数据库失败"+err.Message)) ;
message = "备份数据库失败:" +err.Message;
return false ;
}
finally
{
svr.DisConnect() ;
}
}
/// <summary>
/// 显示进度条的当前进度
/// </summary>
/// <param name="message"></param>
/// <param name="percent"></param>
private void Step(string message,int percent)
{
PBar.Value = percent ;
}
// 其中,这两个语句实现了进度的实时显示:
// SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step);
// bak.PercentComplete += pceh;
// Step就是上面private void Step(string message,int percent) 的方法名称,它用来显示进度条的当前进度。
/// <summary>
/// 数据库的恢复和杀死进程
/// </summary>
/// <param name="strDbName">数据库名称</param>
/// <param name="strFileName">备份文件名</param>
/// <param name="pgbMain">状态条控件名称</param>
/// <param name="strServerName">服务器名称</param>
/// <param name="strUserName">用户名称</param>
/// <param name="strPwd">密码</param>
/// <returns>恢复成功返回true ,否则返回false</returns>
public bool RestoreDB(string strDbName,string strFileName, ProgressBar pgbMain,string strServerName,string strUserName,string strPwd )
{
PBar = pgbMain ;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
ServerName = strServerName ;
UserName = strUserName ;
Password = strPwd ;
svr.Connect(ServerName,UserName,Password) ;
SQLDMO.QueryResults qr = svr.EnumProcesses(-1) ;
int iColPIDNum = -1 ;
int iColDbName = -1 ;
for(int i=1;i<=qr.Columns;i++)
{
string strName = qr.get_ColumnName(i) ;
if (strName.ToUpper().Trim() == "SPID")
{
iColPIDNum = i ;
}
else if (strName.ToUpper().Trim() == "DBNAME")
{
iColDbName = i ;
}
if (iColPIDNum != -1 && iColDbName != -1)
break ;
}
for(int i=1;i<=qr.Rows;i++)
{
int lPID = qr.GetColumnLong(i,iColPIDNum) ;
string strDBName = qr.GetColumnString(i,iColDbName) ;
if (strDBName.ToUpper() == strDbName.ToUpper())
svr.KillProcess(lPID) ;
}
SQLDMO.Restore res = new SQLDMO.RestoreClass() ;
res.Action = 0 ;
SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
res.PercentComplete += pceh;
res.Files = strFileName ;
res.Database = strDbName ;
res.ReplaceDatabase = true ;
res.SQLRestore(svr) ;
return true ;
}
catch(Exception err)
{
// throw(new Exception("恢复数据库失败,请关闭所有和该数据库连接的程序!"+err.Message)) ;
message = "恢复数据库失败,请关闭所有和该数据库连接的程序!" +err.Message;
return false;
}
finally
{
svr.DisConnect() ;
}
}
}
}
在ASP.NET页面中实现数据库备份和恢复功能
80酷酷网 80kuku.com