数据库仍为空

发布于 2024-12-27 13:42:45 字数 2526 浏览 1 评论 0原文

步骤 1:我使用此代码片段备份数据库(效果很好)

Public Shared Sub BackupDatabase()
        Dim sConnect As String = My.Settings.LICConnectionString
        Dim dbName As String

        Using cnn As New SqlConnection(sConnect)
            cnn.Open()
            dbName = cnn.Database.ToString()

            Dim sc As New ServerConnection(cnn)
            Dim sv As New Server(sc)

            ' Check that I'm connected to the user instance
            Console.WriteLine(sv.InstanceName.ToString())

            ' Create backup device item for the backup
            Dim bdi As New BackupDeviceItem("C:\Backup\LIC.bak", DeviceType.File)

            ' Create the backup informaton
            Dim bk As New Backup()
            bk.Devices.Add(bdi)
            bk.Action = BackupActionType.Database
            bk.BackupSetDescription = "SQL Express is a great product!"
            bk.BackupSetName = "SampleBackupSet"
            bk.Database = dbName
            bk.ExpirationDate = New Date(2007, 5, 1)
            bk.LogTruncation = BackupTruncateLogType.Truncate

            ' Run the backup
            bk.SqlBackup(sv)
            MsgBox("Your backup is complete.")
        End Using
    End Sub

步骤 2:我删除表中的所有数据(因此数据库为空)。

步骤 3:使用此代码片段从备份文件恢复数据库(它不会抛出任何错误并且执行正常)

 Private Sub Restore(ByVal ConnectionString As String, ByVal DatabaseFullPath As String, ByVal backUpPath As String)
        Using con As New SqlConnection(ConnectionString)
            con.Open()

            Dim UseMaster As String = "USE master"
            Dim UseMasterCommand As New SqlCommand(UseMaster, con)
            UseMasterCommand.ExecuteNonQuery()

            Dim Alter1 As String = "ALTER DATABASE [" & DatabaseFullPath & "] SET Single_User WITH Rollback Immediate"
            Dim Alter1Cmd As New SqlCommand(Alter1, con)
            Alter1Cmd.ExecuteNonQuery()

            Dim Restore As String = "RESTORE DATABASE [" & DatabaseFullPath & "] FROM DISK = N'" & backUpPath & "' WITH  FILE = 1,  NOUNLOAD,  STATS = 10"
            Dim RestoreCmd As New SqlCommand(Restore, con)
            RestoreCmd.ExecuteNonQuery()

            Dim Alter2 As String = "ALTER DATABASE [" & DatabaseFullPath & "] SET Multi_User"
            Dim Alter2Cmd As New SqlCommand(Alter2, con)
            Alter2Cmd.ExecuteNonQuery()

            MsgBox("Successful")
        End Using
    End Sub

但它不会显示表中的数据...我的意思是在恢复数据库后我应该得到旧的备份文件中的数据...但数据库表仍然是空的..

你能指导我吗?

STEP 1: I backup the database using this code snippet (It works fine)

Public Shared Sub BackupDatabase()
        Dim sConnect As String = My.Settings.LICConnectionString
        Dim dbName As String

        Using cnn As New SqlConnection(sConnect)
            cnn.Open()
            dbName = cnn.Database.ToString()

            Dim sc As New ServerConnection(cnn)
            Dim sv As New Server(sc)

            ' Check that I'm connected to the user instance
            Console.WriteLine(sv.InstanceName.ToString())

            ' Create backup device item for the backup
            Dim bdi As New BackupDeviceItem("C:\Backup\LIC.bak", DeviceType.File)

            ' Create the backup informaton
            Dim bk As New Backup()
            bk.Devices.Add(bdi)
            bk.Action = BackupActionType.Database
            bk.BackupSetDescription = "SQL Express is a great product!"
            bk.BackupSetName = "SampleBackupSet"
            bk.Database = dbName
            bk.ExpirationDate = New Date(2007, 5, 1)
            bk.LogTruncation = BackupTruncateLogType.Truncate

            ' Run the backup
            bk.SqlBackup(sv)
            MsgBox("Your backup is complete.")
        End Using
    End Sub

STEP 2: I delete all the data from the tables(so the database is empty).

STEP 3: Restore the database from the backup file using this code snippet (it does not throw any error and executes fine)

 Private Sub Restore(ByVal ConnectionString As String, ByVal DatabaseFullPath As String, ByVal backUpPath As String)
        Using con As New SqlConnection(ConnectionString)
            con.Open()

            Dim UseMaster As String = "USE master"
            Dim UseMasterCommand As New SqlCommand(UseMaster, con)
            UseMasterCommand.ExecuteNonQuery()

            Dim Alter1 As String = "ALTER DATABASE [" & DatabaseFullPath & "] SET Single_User WITH Rollback Immediate"
            Dim Alter1Cmd As New SqlCommand(Alter1, con)
            Alter1Cmd.ExecuteNonQuery()

            Dim Restore As String = "RESTORE DATABASE [" & DatabaseFullPath & "] FROM DISK = N'" & backUpPath & "' WITH  FILE = 1,  NOUNLOAD,  STATS = 10"
            Dim RestoreCmd As New SqlCommand(Restore, con)
            RestoreCmd.ExecuteNonQuery()

            Dim Alter2 As String = "ALTER DATABASE [" & DatabaseFullPath & "] SET Multi_User"
            Dim Alter2Cmd As New SqlCommand(Alter2, con)
            Alter2Cmd.ExecuteNonQuery()

            MsgBox("Successful")
        End Using
    End Sub

But it does not show the data in the tables...i mean after i restore the database i should get the old data from the backup file ...but the database tables remains empty..

Can u guide me on this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤仙姿 2025-01-03 13:42:45

我相信你的问题是你试图在 SQL 语句中使用数据库路径而不是数据库名称。

假设您的连接字符串正在打开您希望最初恢复到的数据库,则恢复方法可以重写如下:

Private Sub Restore(ByVal ConnectionString As String, ByVal DatabaseFullPath As String, ByVal backUpPath As String)
    Using con As New SqlConnection(ConnectionString)
        con.Open()

        Dim sDatabaseName As String

        sDatabaseName = con.Database

        Dim UseMaster As String = "USE master"
        Dim UseMasterCommand As New SqlCommand(UseMaster, con)
        UseMasterCommand.ExecuteNonQuery()

        Dim Alter1 As String = "ALTER DATABASE [" & sDatabaseName & "] SET Single_User WITH Rollback Immediate"
        Dim Alter1Cmd As New SqlCommand(Alter1, con)
        Alter1Cmd.ExecuteNonQuery()

        Dim Restore As String = "RESTORE DATABASE [" & sDatabaseName & "] FROM DISK = N'" & backUpPath & "' WITH  FILE = 1,  NOUNLOAD,  STATS = 10"
        Dim RestoreCmd As New SqlCommand(Restore, con)
        RestoreCmd.ExecuteNonQuery()

        Dim Alter2 As String = "ALTER DATABASE [" & sDatabaseName & "] SET Multi_User"
        Dim Alter2Cmd As New SqlCommand(Alter2, con)
        Alter2Cmd.ExecuteNonQuery()

        MsgBox("Successful")
    End Using
End Sub

I believe your issue is that you are trying to use the database path in your SQL statements rather than the database name.

Assuming that your connection string is opening the database that you wish to restore to initially, the restore method can be rewritten as follows:

Private Sub Restore(ByVal ConnectionString As String, ByVal DatabaseFullPath As String, ByVal backUpPath As String)
    Using con As New SqlConnection(ConnectionString)
        con.Open()

        Dim sDatabaseName As String

        sDatabaseName = con.Database

        Dim UseMaster As String = "USE master"
        Dim UseMasterCommand As New SqlCommand(UseMaster, con)
        UseMasterCommand.ExecuteNonQuery()

        Dim Alter1 As String = "ALTER DATABASE [" & sDatabaseName & "] SET Single_User WITH Rollback Immediate"
        Dim Alter1Cmd As New SqlCommand(Alter1, con)
        Alter1Cmd.ExecuteNonQuery()

        Dim Restore As String = "RESTORE DATABASE [" & sDatabaseName & "] FROM DISK = N'" & backUpPath & "' WITH  FILE = 1,  NOUNLOAD,  STATS = 10"
        Dim RestoreCmd As New SqlCommand(Restore, con)
        RestoreCmd.ExecuteNonQuery()

        Dim Alter2 As String = "ALTER DATABASE [" & sDatabaseName & "] SET Multi_User"
        Dim Alter2Cmd As New SqlCommand(Alter2, con)
        Alter2Cmd.ExecuteNonQuery()

        MsgBox("Successful")
    End Using
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文