📄 dialogrestore.frm
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
Begin VB.Form DialogRestore
BorderStyle = 3 'Fixed Dialog
Caption = "restore database"
ClientHeight = 2985
ClientLeft = 4665
ClientTop = 3495
ClientWidth = 6615
LinkTopic = "Form1"
MaxButton = 0 'False
MDIChild = -1 'True
MinButton = 0 'False
ScaleHeight = 2985
ScaleWidth = 6615
ShowInTaskbar = 0 'False
Begin VB.ComboBox Combo2
Height = 315
Left = 2640
TabIndex = 8
Top = 960
Width = 2895
End
Begin VB.TextBox Text3
Height = 375
Left = 2640
Locked = -1 'True
TabIndex = 7
Top = 1560
Width = 2535
End
Begin VB.ComboBox Combo1
Height = 315
Left = 2640
TabIndex = 6
Top = 240
Width = 2895
End
Begin MSComDlg.CommonDialog CommonDialog2
Left = 120
Top = 2160
_ExtentX = 847
_ExtentY = 847
_Version = 393216
Filter = "sql 备份文件(*.mdf)|*.mdf|所有文件(*.*)|*.*"
End
Begin VB.CommandButton OKButton
Caption = "确定"
Height = 375
Left = 1200
TabIndex = 1
Top = 2280
Width = 1215
End
Begin VB.CommandButton CancelButton
Caption = "取消"
Height = 375
Left = 3960
TabIndex = 3
Top = 2280
Width = 1215
End
Begin VB.CommandButton Command2
Caption = "..."
Height = 375
Left = 5760
TabIndex = 0
Top = 1560
Width = 615
End
Begin VB.Label Label1
Caption = "请输入备份的数据源:"
Height = 255
Left = 480
TabIndex = 5
Top = 960
Width = 2175
End
Begin VB.Label Label2
Caption = "请选择备份设备:"
Height = 255
Left = 600
TabIndex = 4
Top = 240
Width = 2055
End
Begin VB.Label Label3
Caption = "请选择数据库的恢复路径:"
Height = 255
Left = 480
TabIndex = 2
Top = 1680
Width = 2175
End
End
Attribute VB_Name = "DialogRestore"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim file As Integer
Dim rst As ADODB.Recordset
Dim comm As ADODB.command
Dim dbname As String
Dim sfile As String
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub Combo1_Click()
Dim n As Integer
Combo2.Clear
comm.CommandText = "restore headeronly from [" + Me.Combo1.Text + "]"
Set rst = comm.Execute(, , 1)
n = -1
While Not rst.EOF
Combo2.AddItem " "
n = n + 1
rst.MoveNext
Wend
rst.MoveFirst
While n >= 0
Combo2.RemoveItem n
Combo2.AddItem Trim _
(rst.Fields("backupname")) & " | " & rst.Fields("backupfinishdate") & " | " & rst.Fields("databasename"), n
rst.MoveNext
n = n - 1
Wend
End Sub
Private Sub Combo2_Click()
If Combo1.Text = "" Then
MsgBox "请选择备份设备", 0, "错误信息"
Else
file = Combo2.ListCount - Me.Combo2.ListIndex
comm.CommandText = "restore headeronly from " + Me.Combo1.Text
Set rst = comm.Execute(, , 1)
rst.Move file - 1
dbname = Trim(rst.Fields("databasename"))
Me.Text3.Text = ""
End If
End Sub
Private Sub Command2_Click()
With CommonDialog2
.DialogTitle = "打开"
.CancelError = False
.Filter = "sql 备份文件(*.mdf)|*.mdf|所有文件(*.*)|*.*"
'ToDo: 设置 common dialog 控件的标志和属性
.FileName = dbname
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sfile = .FileName
End With
Me.Text3.Text = sfile
Dim start As Integer
On Error GoTo er
start = 1
While InStr(start + 1, sfile, "\", vbTextCompare) <> 0
start = InStr(start + 1, sfile, "\", vbTextCompare)
Wend
sfile = Right$(sfile, Len(sfile) - start)
sfile = Left$(sfile, Len(sfile) - 4)
Exit Sub
er:
MsgBox "请输入正确的盘符路径!", 0, "错误信息"
End Sub
Private Sub Form_Load()
Dim n As Integer
Set rst = New ADODB.Recordset
Set comm = New ADODB.command
comm.ActiveConnection = adoconn
comm.CommandText = "select *from sysdevices where cntrltype=2"
Set rst = comm.Execute(, , 1)
n = 0
While Not rst.EOF
Combo1.AddItem rst.Fields("name"), n
n = n + 1
rst.MoveNext
Wend
End Sub
Private Sub OKButton_Click()
Dim result As String
If Me.Combo2.Text = "" Then
result = MsgBox("请输入恢复的数据库名", 0, "错误信息")
Else
If Me.Combo1.Text = "" Then
result = MsgBox("请选择备份设备", 0, "错误信息")
Else
If Me.Text3.Text = "" Then
result = MsgBox("请选择数据库的恢复路径", 0, "错误信息")
Else
comm.CommandText = "use master select phyname from sysdevices where name='" + Me.Combo1.Text + "'"
Set rst = comm.Execute(, , 1)
Dim command As String
'file = Combo2.ListIndex + 1
On Error GoTo er
command = "RESTORE DATABASE [" + sfile + "] FROM DISK = N'" + rst.Fields("phyname") + "' WITH FILE = " & file & ",NOUNLOAD,STATS = 10,RECOVERY,REPLACE,MOVE N'" + dbname + "' TO N'" + Left$(Me.Text3.Text, Len(Me.Text3.Text) - 4) + ".mdf',Move N'" + dbname + "_log' TO N'" + Left$(Me.Text3.Text, Len(Me.Text3.Text) - 4) + "_log.ldf'"
If Srun(command) = 1 Then
Unload Me
End If
End If
End If
End If
Exit Sub
er:
Dim enumber As String
enumber = err.Number
If enumber = -2147217900 Then
command = "RESTORE DATABASE [" + sfile + "] FROM DISK = N'" + rst.Fields("phyname") + "' WITH FILE = " & file & ",NOUNLOAD,STATS = 10,RECOVERY,REPLACE,MOVE N'" + dbname + "_data' TO N'" + Left$(Me.Text3.Text, Len(Me.Text3.Text) - 4) + "_data.mdf',Move N'" + dbname + "_log' TO N'" + Left$(Me.Text3.Text, Len(Me.Text3.Text) - 4) + "_log.ldf'"
comm.CommandText = command
comm.Execute , , 1
fMainForm.MousePointer = 0 '改变鼠标样子
MsgBox " successfully completed!!!", 0
Unload Me
Else
fMainForm.MousePointer = 0 '改变鼠标样子
MsgBox err.Description, 0, "错误信息"
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -