我创建了一个具有备份和还原功能的系统。我的备份工作正常,但我的恢复不是。我在整个系统中单独尝试了这个并且它有效。我已经检查是否有连接仍然打开。
首先恢复数据库我需要选择所有可用的本地驱动器。然后会弹出另一个表单并显示所选驱动器中的所有备份文件。
这是我在frmRestore中的代码(在哪里选择驱动器):
Imports System.IO
Imports System.Data.SqlClient
Public Class frmRestore
Dim con As SqlConnection = New SqlConnection
Dim cmd As SqlCommand
Dim dread As SqlDataReader
Private Sub frmRestore_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
frmMain1.unlockmenu()
End Sub
Private Sub frmBackupRestore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim alldrives() As DriveInfo = DriveInfo.GetDrives()
For Each d As DriveInfo In alldrives
If d.IsReady = True Then
ComboBox1.Items.Add(d.Name & " " & d.VolumeLabel)
End If
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If ProgressBar1.Value = 100 Then
Timer1.Enabled = False
ProgressBar1.Visible = False
MsgBox("Successfully Done")
Else
ProgressBar1.Value = ProgressBar1.Value + 5
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pat As String = ComboBox1.Text & "POSASBACK"
If Not System.IO.Directory.Exists(pat) Then
MsgBox("No backup files to restore")
Exit Sub
End If
frmRestoreList.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
这是我在frmRestoreList中的代码(在哪里选择备份数据文件):
Imports System.IO
Imports System.Data.SqlClient
Public Class frmRestoreList
Dim con As SqlConnection = New SqlConnection
Dim cmd As SqlCommand
Dim dread As SqlDataReader
Sub query(ByVal que As String)
On Error Resume Next
cmd = New SqlCommand(que, con)
cmd.ExecuteNonQuery()
con.Close()
End Sub
Private Sub frmRestoreList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim targetDirectory As String = frmRestore.ComboBox1.Text & "POSASBACK"
Dim fileEntries As String() = System.IO.Directory.GetFiles(targetDirectory, "*.bak")
Dim filedate As System.IO.FileInfo
Dim fileName As String
For Each fileName In fileEntries
filedate = My.Computer.FileSystem.GetFileInfo(fileName)
DataGridView1.Rows.Add(fileName, Replace(fileName, targetDirectory & "\", ""), Format(filedate.LastWriteTime, "MMMM dd,yyyy (dddd)"))
Next fileName
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If MsgBox("Are you sure you want to proceed with the data file' restore?" & vbNewLine & "This will overwrite your data files in the Back-Up file.", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "") = MsgBoxResult.Yes Then
con = New SqlConnection("Data Source=.\SQLEXPRESS;Database=Master;integrated security=SSPI;")
con.Open()
query("restore database dbbotika FROM DISK='" & DataGridView1.SelectedRows(0).Cells(0).Value & "' with replace")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
请提前帮助我们。
答案 0 :(得分:4)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If MsgBox("Are you sure you want to proceed with the data file' restore?" & vbNewLine & "This will overwrite your data files in the Back-Up file.", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "") = MsgBoxResult.Yes Then
con = New SqlConnection("Data Source=.\SQLEXPRESS;Database=Master;integrated security=SSPI;")
con.Open()
'Set the DB to the (master) DB => If the used DB was the DB that you want to Restore then an error will occure
query("USE [master] ")
'Drop the connection to the DB by setting the connection to your session only (single user), any current transaction on the DB => it will be rolledback immediatelly
query("ALTER DATABASE dbbotika set SINGLE_USER WITH ROLLBACK IMMEDIATE")
'Restore DB
query("restore database dbbotika FROM DISK='" & DataGridView1.SelectedRows(0).Cells(0).Value & "' with replace")
'Return the connection to the DB to be multi users
query("ALTER DATABASE dbbotika SET MULTI_USER")
'Use your DB name
query("USE [dbbotika] ")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub