今天做一个恢复数据库的程序时,突然想起 sqlserver 有一个功能强大的组件 sqldmo ,与是查阅了一下它的相关帮助,发现它刚好能够满足我的需求:
下面是我的程序的部代码,在这里共享组大家分享。
以下代码在 vb6中文版中编译通过!
启动数据服务:
public sub start_server()
dim svr as new sqldmo.sqlserver
on error goto errhwnd
svr.start false, myserver
svr.close
exit sub
errhwnd:
if err.number <> -2147023840 then
msgbox err.number & vbcrlf & err.description, vbexclamation, "错误"
end if
end sub
联接并验证密码:
public function connserver(byval sqlserver as string, byval uid as string, byval pwd as string) as boolean
dim st as sqldmo_svcstatus_type
dim svr as new sqldmo.sqlserver
on error goto errhwnd
svr.logintimeout = 10
svr.connect sqlserver, uid, pwd
connserver = true
exit function
errhwnd:
if err.number = -2147203048 then
msgbox "登录失败密码不正确,请重新输入!", vbinformation, "提示"
else
resume next
end if
end function
恢复数据库时如果要重新部署数据库特理文件位置需注意设置
恢复数据库:
public withevents ores as sqldmo.restore '声明事件注意此行放在过程调用之前
private sub restoredata(byval datafile as string)
on error goto errhwd
dim osql as sqldmo.sqlserver
set ores = new sqldmo.restore
set osql = new sqldmo.sqlserver
osql.loginsecure = false
osql.connect servername, sqluser, sqlpwd '联接数据服务
ores.action = sqldmorestore_database '恢复类型数据库
ores.database = "rcrs" '数据库名称
ores.replacedatabase = true '替代现有数据库如不存则创建它
ores.files = datafile '备份文件名,如果是多个设备上的需使用.devices属性
ores.databasefiles = datafile '数据库文件此参数必须设置否则无法使用 relocatefiles
ores.filenumber = 1 '文件在设备上的id号如果你只有一个文件就是1
'relocatefiles 重新部署数据库逻辑名'"[逻辑名],[物理文件名地址]"
ores.relocatefiles = "[tyrs2_data]" & "," & "[" & app.path & "/rcrs_data.mdf]," & _
"[tyrs2_log]" & "," & "[" & app.path & "/rcrs_log.ldf]"
doevents
ores.sqlrestore osql '调用恢复方法
set ores = nothing
set osql = nothing
exit sub
errhwd:
select case err.number
case -2147221499
'这个错误尚不知道原因,但不影响操作结果
resume next
case -2147218403
msgbox "数据库正在使用,请关闭所有正在使用数据的程序!", vbexclamation, "错误"
case else
msgbox err.number & vbcrlf & err.description, vbexclamation, "提示"
end select
end sub
'恢复事件
private sub ores_percentcomplete(byval message as string, byval percent as long)
probar.value = probar.max * (percent / 100) '设置进度条
end sub
新闻热点
疑难解答