1

我正在使用此代碼恢復SQL Server數據庫服務器xxxx/SQLExpress的還原失敗

Server databaseServer = new Server(new ServerConnection(CvVariables.SQL_SERVER_NAME)); 
string databasePath = System.IO.Path.Combine(Environment.CurrentDirectory,); 
string databasePath = @"D:\cvdb.bak"; 

Restore databaseRestore = new Restore(); 
databaseRestore.Action = RestoreActionType.Database; 
databaseRestore.Database = CvVariables.Catalog; 
databaseRestore.Devices.Add(new BackupDeviceItem(databasePath, DeviceType.File)); 
databaseRestore.ReplaceDatabase = true; 
databaseRestore.SqlRestore(databaseServer); 

我使用的是代號爲'Denali'Express Core(CTP 3)的SQL Server。

此代碼在開發人員PC上工作正常,但在客戶端的PC上,它會拋出此異常:

服務器xxxx/SQLExpress

恢復失敗我不明白我在哪裏錯誤。

InnerException是:

Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. ---> System.Data.SqlClient.SqlException: Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Cafeteria_Vernier_db.mdf" failed with the operating system error 3(The system cannot find the path specified.). 
File 'Cafeteria_Vernier_db' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Cafeteria_Vernier_db.mdf'. Use WITH MOVE to identify a valid location for the file. 
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Cafeteria_Vernier_db_log.ldf" failed with the operating system error 3(The system cannot find the path specified.). 
File 'Cafeteria_Vernier_db_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Cafeteria_Vernier_db_log.ldf'. Use WITH MOVE to identify a valid location for the file. 
Problems were identified while planning for the RESTORE statement. Previous messages provide details. 
RESTORE DATABASE is terminating abnormally. 
    at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException) 
    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType) 
    --- End of inner exception stack trace --- 
    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType) 
    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType) 
    at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries) 
    at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server server, StringCollection queries) 
    at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv) 
+0

服務器是否是相同的版本。 SQL Server不喜歡從較新的SQL Server版本恢復備份。信息消息中通常有更多的信息。您可以從Management Studio恢復服務器上的備份嗎? –

+0

是的,兩者都是相同的版本,是的我從Management Studio存儲在服務器上的備份。 – Vero009

+0

您是否在異常情況,狀態代碼,內部異常中獲得更多信息? –

回答

3

這聽起來像你的目標機器上的路徑是從原來的備份機上的相應路徑不同。

建議: 我忘了.Net API的東西,並直接下降到T-SQL(當然,你可以從C#做到這一點)。

你新的T-SQL腳本將是這個樣子:

-- REFERENCE: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=147723 
RESTORE DATABASE paintcheck 
FROM DISK = 'C:\paintcheck.BAK' 
WITH REPLACE, MOVE 'paintcheck' TO 'C:\MSSQL\DATA\paintcheck_Data.MDF', 
MOVE 'paintcheck_log' TO 'C:\MSSQL\DATA\paintcheck_Log.LDF' 

下面是一些例子:

http://geekswithblogs.net/AskPaula/archive/2011/07/11/146167.aspx

http://msdn.microsoft.com/en-us/library/ms186858.aspx

「希望幫助

+0

非常有幫助。謝謝。 – Emmanuel

0

您必須重新安置數據庫文件使用以下代碼:

Server databaseServer = new Server(new ServerConnection(CvVariables.SQL_SERVER_NAME)); 
string databasePath = @"D:\cvdb.bak"; 

// Generate new FilePath for both Files. 
string fileMdf = System.IO.Path.Combine(databaseServer.MasterDBPath, "Cafeteria_Vernier_db.mdf"); 
string fileLdf = System.IO.Path.Combine(databaseServer.MasterDBLogPath, "Cafeteria_Vernier_db_log.ldf"); 
RelocateFile relocateMdf = new RelocateFile("Cafeteria_Vernier_db", fileMdf); 
RelocateFile relocateLdf = new RelocateFile("Cafeteria_Vernier_db_log", fileLdf); 

Restore databaseRestore = new Restore(); 
databaseRestore.Action = RestoreActionType.Database; 
databaseRestore.Database = CvVariables.Catalog; 
databaseRestore.Devices.Add(new BackupDeviceItem(databasePath, DeviceType.File)); 
databaseRestore.RelocateFiles.Add(relocateMdf); 
databaseRestore.RelocateFiles.Add(relocateLdf); 
databaseRestore.ReplaceDatabase = true; 
databaseRestore.SqlRestore(databaseServer);