求助?SQLDMO 恢复数据库时出错!
求助?本人在做 SQLDMO 恢复数据库时出错!报以下错误:[Microsoft][ODBC SQL Server Driver][SQL Server]无法重写文件 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\aaaa_Data.MDF'。数据库 'aaaa' 正在使用该文件。 [Microsoft][ODBC SQL Server Driver][SQL Server]文件 'aaaa_Data' 无法还原为 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\aaaa_Data.MDF'。请使用 WITH MOVE 选项来标识该文件的有效位置。 [Microsoft][ODBC SQL Server Driver][SQL Server]无法重写文件 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\aaaa_Log.LDF'。数据库 'aaaa' 正在使用该文件。 [Microsoft][ODBC SQL Server Driver][SQL Server]文件 'aaaa_Log' 无法还原为 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\aaaa_Log.LDF'。请使用 WITH MOVE 选项来标识该文件的有效位置。 [Microsoft][ODBC SQL Server Driver][SQL Server]RESTORE DATABASE 操作异常终止。
整个程序代码如下:
Imports System.Data
Imports System.Data.SqlClient
‘备份数据库
Public Class Form1
Private Sub btnServer1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnServer1.Click
Dim backup As New SQLDMO.Backup
Dim sqlserver As New SQLDMO.SQLServer
Try
sqlserver.LoginSecure = False
sqlserver.Connect(".", "sa", "")
backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database
backup.Database = "aaaa" '数据库名称
backup.Files = "c:\databak.bak"
backup.BackupSetName = "aaaa_bak"
backup.BackupSetDescription = "数据库备份"
backup.Initialize = True
backup.SQLBackup(sqlserver)
Catch ex As Exception
Throw ex
Finally
sqlserver.DisConnect()
End Try
End Sub
’恢复数据库
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim restore As New SQLDMO.Restore
Dim sqlserver As New SQLDMO.SQLServer
Try
sqlserver.LoginSecure = False
sqlserver.Connect(".", "sa", "")
restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database
restore.Database = "aaaa_bak"
restore.Files = "c:\databak.bak"
restore.FileNumber = 1
restore.ReplaceDatabase = True '操作将覆盖原有同名数据库
restore.SQLRestore(sqlserver)
MsgBox("恢复成功!", MsgBoxStyle.OkOnly)
Catch ex As Exception
Throw ex
Finally
sqlserver.DisConnect()
End Try
End Sub
End Class