簡體   English   中英

SQL Restore數據庫在VB中不起作用

[英]SQL Restore Database doesn't work in 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

幫幫我,謝謝。

在此處輸入圖片說明在此處輸入圖片說明

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM