public void RestoreDatabase(String databaseName, String filePath,
String serverName, String userName, String password, String dataFilePath, String logFilePath)
{
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
sqlRestore.Action = RestoreActionType.Database;
String dataFileLocation = dataFilePath;
String logFileLocation = logFilePath;
db = sqlServer.Databases[databaseName];
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation));
sqlRestore.ReplaceDatabase = true;
sqlRestore.Complete +=new ServerMessageEventHandler(sqlRestore_Complete);
sqlRestore.SqlRestore(sqlServer);
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}
在调用此方法时,恢复操作失败并显示此消息
服务器恢复失败 'MDM04 \ SQLEXPRESS'。
mdm04是我的电脑名称
内部异常是
“System.Data.SqlClient.SqlError: 逻辑文件'vrv'不属于 数据库'vrv'。使用RESTORE FILELISTONLY列出逻辑文件 名“。
vrv是数据库名称
我该怎么做才能恢复文件
答案 0 :(得分:15)
问题在这里
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation));
这里databaseName表示db备份文件中指定的数据库的名称。但是您要指定目标数据库名称。
将其更改为原始数据库名称
这里是从备份文件中读取数据库名称的示例代码
DataTable dtFileList = sqlRestore.ReadFileList(serverName);
string dbLogicalName = dtFileList.Rows[0][0].ToString();
string dbPhysicalName = dtFileList.Rows[0][1].ToString();
string logLogicalName = dtFileList.Rows[1][0].ToString();
string logPhysicalName = dtFileList.Rows[1][1].ToString
答案 1 :(得分:0)
现在转到“浏览”标签并浏览以下路径 -
C:\ Program Files \ Microsoft SQL Server \ 100 \ SDK \ Assemblies 要么 C:\ Program Files \ Microsoft SQL Server \ 110 \ SDK \ Assemblies
现在选择以下dll
Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Management.Sdk.Sfc.dll
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.SmoExtended.dll
Microsoft.SqlServer.SqlEnum.dll
public void RestoreDatabase(String databaseName, String filePath,
String serverName, String userName, String password,
String dataFilePath, String logFilePath)
{
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
sqlRestore.Action = RestoreActionType.Database;
String dataFileLocation = dataFilePath;
String logFileLocation = logFilePath;
db = sqlServer.Databases[databaseName];
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation));
sqlRestore.ReplaceDatabase = true;
sqlRestore.Complete +=new ServerMessageEventHandler(sqlRestore_Complete);
sqlRestore.SqlRestore(sqlServer);
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}