C#备份及恢复数据库

C#备份及恢复数据库,第1张

1. 在用户的配置时,我们需要列出当前局域网内所有的数据库服务器,并且要列出指定服务器的所有数据库,实现代码如下:

取得数据库服务器列表:

public ArrayList GetServerList()

{

ArrayList alServers = new ArrayList()

SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass()

try

{

SQLDMO.NameList serverList = sqlApp.ListAvailableSQLServers()

for(int i = 1i<= serverList.Counti++)

{

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=1i<=qr.Columnsi++)

{

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=1i<=qr.Rowsi++)

{

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=1i<=qr.Columnsi++)

{

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=1i<=qr.Rowsi++)

{

int lPID = qr.GetColumnLong(i,iColPIDNum)

string strDBName = qr.GetColumnString(i,iColDbName)

if (strDBName.ToUpper() == strDbName.ToUpper())

svr.KillProcess(lPID)

}

出现这个问题一般有三种情况:

1.你的数据库正在占用,

2.你的备份文件太大

3.你存储备份文件的硬盘正忙

我感觉你的数据库像是在占用,一般连接数据库的程序,或是开发环境打开着也会导致数据库占用。建议先分离或删除原数据库再还原,因为分离和删除有强制关闭连接


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9950892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存