Sql server的協助文檔地址,在IE裡面直接輸入就行
mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\tsqlref.chm::/ts_ba-bz_35ww.htm
backup database to disk path備份
restore database from disk path恢複
using System;
using System.Collections;
using System.Security.Cryptography;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics ;
using System.Text ;
using System.ComponentModel;
using System.Configuration;
using System.Data.OleDb;
namespace DbService
{
/// <summary>
/// DbOper類,主要實現對Microsoft SQL Server資料庫的備份和恢複
/// </summary>
public sealed class DbOper
{
/// <summary>
/// DbOper類的建構函式
/// </summary>
//private DbOper()
//{
//}
/// <summary>
/// Database Backup
/// </summary>
///
public static string BackFileName=System.Web.HttpContext.Current.Request.PhysicalApplicationPath+"BackUp\\MyDbBack.bak";
public static void DbBackup(string DbNanme,string userid,string pwd)
{
try
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost",userid,pwd);
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = DbNanme;
oBackup.Files =BackFileName;
oBackup.BackupSetName =DbNanme;
oBackup.BackupSetDescription = "Database Backup";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch
{
throw;
}
}
/// <summary>
/// 還原資料庫函數
/// </summary>
/// <param name="strDbName">資料庫名</param>
/// <param name="strFileName">Database Backup檔案的完整路徑名</param>
/// <returns></returns>
public bool RestoreDB(string strDbName,string strFileName,string userid,string pwd)
{
//PBar = pgbMain ;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
//伺服器名,資料庫使用者名稱,資料庫使用者名稱密碼
svr.Connect("localhost",userid,pwd) ;
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 ;
res.Files = strFileName ;
res.Database = strDbName ;
res.ReplaceDatabase = true ;
res.SQLRestore(svr) ;
return true ;
}
catch
{
return false;
}
finally
{
svr.DisConnect() ;
}
}
}
}