用VB如何备份和还原SQL Server数据库????

cjwcyc 2003-01-20 04:33:58
用VB如何备份和还原SQL Server数据库????
...全文
16 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xks 2003-03-25
Dim cn As New ADODB.Connection
Dim s_path, s_dataexport As String
s_path = App.path
Me.MousePointer = 11
s_dataexport = "backup database database_name to disk='" + s_path + "\backup\" + Format(Now(), "YY-MM-DD") + userinfo.Depname + "backup.dat'"
cn.Open gs_conn_string
cn.BeginTrans
cn.Execute s_dataexport
Err.Number = 0
If Err.Number = 0 Then
cn.CommitTrans
MsgBox "数据备份成功!", vbInformation, "提示"
Else
cn.RollbackTrans
MsgBox "数据备份失败!", vbCritical, "提示"
End If
cn.Close
Set cn = Nothing
Me.MousePointer = 1


Dim ret As Integer
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
'Debug.Print gs_conn_string
'此时需要连接master数据库才能完成数据恢复操作
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=7;Initial Catalog=master;Data Source=127.0.0.1 "
'同上student1为需要恢复的数据库
s_restore = "restore database student1 from disk='" + Trim(Text1.Text) + "'"
'text1一个用于记录需要恢复文件的地址的textbox
cn.Execute s_restore
cn.BeginTrans
If Err.Number = 0 Then
cn.CommitTrans
MsgBox "数据恢复成功!", vbInformation, "提示"
Else
cn.RollbackTrans
MsgBox "数据恢复失败!", vbCritical, "提示"
End If
cn.Close
Set cn = Nothing
Me.MousePointer = 1
Else
Exit Sub
End If
End If
回复
jiang_nan 2003-01-20
建议备份文件的文件名带上当时的系统日期:db & format("yyyy-mm-dd",date) & "-" & format("hh-mm-ss",time)
回复
nik_Amis 2003-01-20
sSQL = "RESTORE DATABASE " & sDatabaseName & " FROM DISK=" & Wrap(sDatabaseFile) & " WITH REPLACE "
sSQL = "BACKUP DATABASE " & sDatabaseName & " TO DISK=" & Wrap(sDatabaseFile) & " WITH INIT"

回复
maoqingfeng 2003-01-20
用 backup restore sql语句
回复
antshome 2003-01-20
强烈建议参考SQLDMO的例子,在SQL 2000安装盘里DEVTOOLS\SAMPLES\目录下
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告