【问题标题】:SQL Restore Database doesn't work in vbSQL 还原数据库在 vb 中不起作用
【发布时间】:2014-10-08 07:18:17
【问题描述】:

我创建了一个具有备份和恢复功能的系统。我的备份工作正常,但我的恢复却不行。我在整个系统中单独尝试过,它可以工作。我已经检查是否有仍然打开的连接。

首先在恢复数据库时,我需要选择所有可用的本地驱动器。然后会弹出另一个窗体并显示所选驱动器内的所有备份文件。

这是我在 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

帮帮我,提前谢谢。

【问题讨论】:

  • “不起作用”不适用于诊断问题 - 我注意到您有捕获并显示异常的代码 - 发生了吗?如果是这样,会显示什么错误消息?您是否尝试过在管理工作室中运行 SQL - 它在那里工作?
  • 我在 sql 管理中试过这个,它可以工作。就像我说的那样,这是系统的一部分,当我将它分离并运行它时。它有效。

标签: sql vb.net sql-server-2008 backup restore


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多