用asp.net代码怎样实现数据库备份和还原,急急急!!!!
用asp.net代码怎样实现数据库备份和还原,这个我现在已经实现了,代码是网上找的,代码如下:
/// <summary>
/// 还原数据库函数
/// </summary>
/// <param name= "strDbName "> 数据库名 </param>
/// <param name= "strFileName "> 数据库备份文件的完整路径名 </param>
/// <returns> </returns>
public bool RestoreDB(string strDbName,string strFileName)
{
//PBar = pgbMain ;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
//服务器名,数据库用户名,数据库用户名密码
svr.Connect( "localhost ", "sa ", "hai ") ;
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 ;
}
//杀死使用strDbName数据库的进程
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 ;