2014-10-08 82 views
-1

我創建了一個具有備份和恢復功能的系統。我的備份工作正常,但我的恢復不是。我在整個系統中單獨嘗試了它,並且它可以工作。我已經檢查是否有連接仍然打開。SQL恢復數據庫不能在vb

在恢復數據庫首先我需要選擇所有可用的本地驅動器。然後另一個表單將彈出並顯示所選驅動器內的所有備份文件。

這裏是我的代碼在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 

幫助我的傢伙,提前致謝。

enter image description here

+0

「不工作」是不是很大了診斷的問題 - 我注意到你有捕捉和顯示異常代碼 - 這是怎麼回事?如果是這樣,顯示什麼錯誤信息?您是否嘗試過在管理工作室中運行SQL - 它在那裏工作? – 2014-10-08 07:33:50

+0

我在sql管理中試過這個,它工作。就像我所說的那樣,這是系統的一部分,當我將它分開並運行時。有用。 – user3579618 2014-10-08 08:06:47

回答

3
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