服务器xxxx / SQLExpress的还原失败

时间:2011-11-13 06:47:49

标签: c# sql-server sql-server-2008 sql-server-2005 sql-server-2012

我正在使用此代码恢复SQL Server数据库

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 Is:

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)

2 个答案:

答案 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

'希望有所帮助

答案 1 :(得分: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);