c備份還原資料庫
備份資料庫
使用mysqlmp命令備份資料庫
Ⅱ C#備份及恢復資料庫
1. 在用戶的配置時,我們需要列出當前區域網內所有的資料庫伺服器,並且要列出指定伺服器的所有資料庫,實現代碼如下:
取得資料庫伺服器列表:
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)) ;
}
finally
{
sqlApp.Quit() ;
}
return alServers ;
}
取得指定資料庫伺服器的資料庫列表
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 e)
{
throw(new Exception("連接資料庫出錯:"+e.Message)) ;
}
finally
{
svr.DisConnect() ;
sqlApp.Quit() ;
}
return alDbs ;
}
2. 資料庫的備份和實時進度顯示代碼:
public bool BackUPDB(string strDbName,string strFileName, ProgressBar pgbMain)
{
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)) ;
}
finally
{
svr.DisConnect() ;
}
}
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) 的方法名稱,它用來顯示進度條的當前進度。
3. 資料庫的恢復和殺死進程的代碼:
public bool RestoreDB(string strDbName,string strFileName, ProgressBar pgbMain)
{
PBar = pgbMain ;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
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)) ;
}
finally
{
svr.DisConnect() ;
}
}
其中這個語句取得了所有的進程列表:
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) ;
}
Ⅲ 簡述資料庫備份及恢復的方法
我不知道你用的是哪個版本的資料庫~~~如果是SQL2005是這樣操作的
首先右擊你要備份的資料庫---任務---備份---添加---選擇你備份文件的路徑
輸入備份文件名,記住一定要有後綴,也就是
.bak
比如說
aaa。bak
然後一直確定就可以到你的備份路徑下找到備份文件了
Ⅳ 如何把資料庫備份還原到一個新資料庫中
1、隨便選擇一個資料庫來還原,選擇好你的bak文件後,再查看「選項」選項卡中可以查看bak文件關聯的原資料庫名稱。
2、然後取消還原,新建一個空的原名稱的資料庫,再執行還原備份就可以了。
Ⅳ 資料庫如何還原和備份
選擇需要備份的資料庫,右鍵--任務--還原。
如何備份與還原資料庫
如圖點擊添加,選擇我們需要備份到哪裡。
如何備份與還原資料庫
如下圖選擇好路徑後,給備份文件命名。
如何備份與還原資料庫
如何備份與還原資料庫
依次點擊確定,完成以後會提示還原成功。
如何備份與還原資料庫
可以把剛剛那邊備份文件還原到原來的資料庫,也可以還原給一個新的空的資料庫。假設我們還原一個新的資料庫。如圖依次展開
如何備份與還原資料庫
勾上選擇源設備--選擇路徑。
如何備份與還原資料庫
通過添加--找到剛剛我們備份的那個文件。
如何備份與還原資料庫
如何備份與還原資料庫
依次點擊確定後,回到還原資料庫界面,勾上還原文件。
如何備份與還原資料庫
在還原資料庫選項中點擊選項,找到我們這個新建的數據的文件和日誌存放路徑。(如果不知道,可以選中該資料庫--屬性--文件裡面找到默認路徑),勾上覆蓋原有資料庫。確定就OK了。
如何備份與還原資料庫