如何用VB代码实现对mssql数据库的备份和恢复?

cd555 2003-03-31 11:03:35
请指点
...全文
26 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cd555 2003-04-03
非常感谢你的热心帮助。
等结帖后我会+分
回复
xks 2003-04-01
一点微薄之荐!

呵呵
回复
xks 2003-04-01
恢复窗体上有两个按钮 打开和确定
一个文本框用显示恢复备份文件的文件名!
确定按钮代码如下:

dim ret as interger
If Text1.Text = "" Then
MsgBox "请选择要恢复的数据文件!", vbInformation, "提示"
Exit Sub
Else
ret = MsgBox("数据恢复操作将会覆盖以前的所有数据并且覆盖后无法恢复,您确定要进行恢复操作吗?", vbQuestion + vbOKCancel, "提示")
If ret = vbOK Then
Me.MousePointer = 11
Dim cn As New ADODB.Connection
Dim s_restore As String
dim sn As New ADODB.Recordset
cn.Open "Provider=SQLOLEDB.1;server=你的计算机名;Persist Security Info=False;Initial Catalog=master;Data Source=127.0.0.1;user id=sa;password=密码"
sn.Open "select spid from sysprocesses where dbid=db_id( 'student')", cn 'student换成你要恢复的数据库名称
Do While Not sn.EOF
cn.Execute "kill " & sn("spid")
sn.MoveNext
Loop
sn.Close
s_restore = "restore database 数据库名 from disk='" + Trim(Text1.Text) + "' with REPLACE"
cn.Execute s_restore
cn.BeginTrans
If Err.Number = 0 Then
cn.CommitTrans
MsgBox "数据恢复成功!", vbInformation, "提示"
Command1.Enabled = True
Else
cn.RollbackTrans
MsgBox "数据恢复失败!", vbCritical, "提示"
Command1.Enabled = True
End If
cn.Close
Set cn = Nothing
Me.MousePointer = 1
Else
Exit Sub
End If
有什么不明白的发消息给我!

以上代码的总体思想是以先断开当前与数据库建立的所有连接!

然后恢复数据库!因为数据库正在使用无法完成恢复数据库功能

所以先断开连接!

这样就不会出现你的错误了!

如果恢复后还要连接上数据库的话再加上以下代码就行了!


On Error Resume Next
Dim DBC As New DataBaseConnection
If db.State = 1 Then
db.Close
End If
db.ConnectionString = DBC.SqlConnectString(d1, d2, d3) 'd1为计算机名,d2为sa,d3为sql 登录密码
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
db.CursorLocation = adUseClient
db.Open
Set cmd.ActiveConnection = db
If Err.Number Then
MsgBox Err.Description, 16 + vbOKOnly, Err.Number
Exit Sub
End If
db.DefaultDatabase = "student1" 'student换成你的数据库名称
If Err.Number Then
MsgBox Err.Description, 16 + vbOKOnly, Err.Number
Exit Sub
End If
End If

这是类模块DataBaseConnection代码

Public Function SqlConnectString(ServerName As String, UserName As String, password As String) As String
SqlConnectString = "Provider=SQLOLEDB.1;User ID=" + UserName + ";Pwd=" + password + ";Data Source=" + ServerName
End Function
回复
cd555 2003-04-01
备份是成功了,但恢复时提示:
因为数据库正在使用,所以未能获得该数据库的排他访问权。
请问怎么解决呢?
回复
ivt 2003-03-31
SQL = "BACKUP DATABASE 数据库名 TO disk='D:\mssql7\BACKUP\数据库名.bak'"
cn.execute sql
SQL = "restore DATABASE 数据库名 from disk='D:\mssql7\BACKUP\数据库名.bak'"
cn.execute sql
回复
wu_yongcai 2003-03-31
up
回复
lihonggen0 2003-03-31
http://expert.csdn.net/Expert/topic/1273/1273804.xml?temp=.8347132
回复
lihonggen0 2003-03-31
restore DATABASE .........恢复
回复
lihonggen0 2003-03-31
sql server 7种的备份方法
If cn.State = adStateOpen Then cn.Close
cn.ConnectionTimeout = 10
connstr = "Driver={SQL Server};Server=" & LocalServer & ";UID=" & LocalUser & ";PWD=" & LocalPw & ";DATABASE=" & LocalDb
cn.Open connstr
Nowtime = Format(Now, "hh-mm-ss")
dd = Str(Date)
riqi = dd + "-" + Nowtime

SQL = "BACKUP DATABASE tablename TO disk='D:\mssql7\BACKUP\tablename" & riqi & "' with noinit"
cn.Execute (SQL)
回复
zsgzsgzsg 2003-03-31
http://expert.csdn.net/Expert/topic/1561/1561295.xml?temp=.1990473
回复
ketao_78 2003-03-31
vb代码?
不用sql语句?
回复
相关推荐
发帖

1187

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2003-03-31 11:03
社区公告
暂无公告