-2
第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
不過,這並不表中顯示的數據。我意思後,我恢復的數據庫中,我應該從備份文件中的舊數據...但數據庫表保持爲空..
u能指導我這個?
你的假設是不正確的;你說這兩個備份和恢復「工作正常」,但這顯然*不真實*。 – 2012-01-15 09:19:28
它不會拋出任何錯誤...任何想法我哪裏錯了? – 2012-01-15 09:23:40
不知道*在所有*,因爲你從來沒有測試過,如果備份*真的工作* ... – 2012-01-15 09:26:25