sql2005資料庫備份與還原

2022-03-05 02:08:02 字數 2168 閱讀 1670

現在我把相關**寫在這兒,以備以後再用

資料庫備份的儲存過程:

set ansi_nulls on

set quoted_identifier on

gocreate  procedure [dbo].[databaseback]

@backid uniqueidentifier,

@strpath nvarchar(200)

asbegin

declare @strpaths nvarchar(200)

set @strpaths = convert(nvarchar(19),getdate(),120)

set @strpaths = replace(@strpaths, ':' , '.')

set @strpaths = replace(@strpaths, ' ' , '.')

set @strpaths = @strpath+'/'+@strpaths + '.bak'

backup database [oms] to disk = @strpaths with noinit , nounload , noskip , stats = 10, noformat

en表示層後台**:

//獲取軟體的根目錄

int pathindex = actualserverpath.lastindexof("\\");

string path = actualserverpath.substring(0, pathindex);   

path += "\\";

//在軟體的根目錄下建資料夾

datetime time = datetime.now;

string paths = "database/" + time.year.tostring();

paths += "/" + time.month.tostring();

//由於程式中的根目錄和建的資料夾路徑中的斜槓不一樣,所以需要轉換(這點特別要注意了)

path += paths.replace('/','\\');

然後直接呼叫儲存過程就ok了。

備份要注意的問題:

1、要備份的資料庫必須和軟體在同一臺機器上

2、備份的檔案必須加字尾名:.bak

3、軟體的根目錄和建的資料夾路徑中的斜槓要轉換一致

資料庫恢復的表示層後台**:

//backconnection是web.config中的連線字串名

database db = databasefactory.createdatabase("backconnection");

string sql = "select spid from master..sysprocesses where dbid=db_id(@dbname)";

dbcommand cmd = db.getsqlstringcommand(sql);

db.addinparameter(cmd, "@dbname", dbtype.string, "oms");

idatareader dr = db.executereader(cmd);

listprocess = new list();

try}

catch

dr.close();

cmd.parameters.clear();

//殺死程序

foreach (string s in process)

//恢復sql語句

sql = "restore database oms from

disk=@path

with replace";

cmd = db.getsqlstringcommand(sql);

//path是要恢復的檔案路徑

db.addinparameter(cmd, "@path", dbtype.string, path);

db.executenonquery(cmd);

cmd.connection.close();

cmd.parameters.clear();

cmd.dispose();

response.write("");

恢復資料庫要注意的問題:

1、要恢復的資料庫跟正在執行的軟體所連線的資料庫不能是同乙個

2、恢復資料庫用master系統資料庫強制殺死軟體正在執行的資料庫程序

sql2005資料庫備份與還原

現在我把相關 寫在這兒,以備以後再用 資料庫備份的儲存過程 set ansi nulls on set quoted identifier ongo create procedure dbo databaseback backid uniqueidentifier,strpath nvarchar ...

sql2005資料庫還原

需要注意的問題 a.兩類特殊的資料型別 日期和時間型別的資料儲存方式和可用值範圍 相關的計算 比較 顯示 換為指定格式的字串 都比較複雜,還涉及一組日期時間函式。參看datetime型別分析一帖。字串型別涉及到字元編碼和排序規則,比較操作還包含like匹配 未來還可能會支援正規表示式匹配 非常需要注...

sql2005資料庫遠端備份

開啟高階設定 exec sp configure show advanced options 1 reconfigure 開啟xp cmdshell擴充套件儲存過程 exec sp configure xp cmdshell 1 reconfigure declare strdirname varc...