我正在尝试使用Microsoft.SqlServer.Management.Smo将数据库从一台服务器还原到另一台服务器。问题是我不断收到错误消息,指出它找不到.MDF文件。这样做的原因是,它试图从它来自的SQL实例名称的数据文件夹中找到它,而不是在另一个SQL实例的数据文件夹中寻找它。有没有一种方法可以告诉它要在哪个文件夹中搜索mdf,而不是假定它是标记到BAK文件的文件夹?真令人沮丧
看看Bappi M Ahmed的这篇文章。它显示了用于还原数据库文件的完整方法。这是它显示的方法:
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 + databaseName + ".mdf"; String logFileLocation = logFilePath + databaseName + "_Log.ldf"; db = sqlServer.Databases[databaseName]; RelocateFile rf = new RelocateFile(databaseName, dataFileLocation); 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.PercentCompleteNotification = 10; sqlRestore.PercentComplete += new PercentCompleteEventHandler(sqlRestore_PercentComplete); sqlRestore.SqlRestore(sqlServer); db = sqlServer.Databases[databaseName]; db.SetOnline(); sqlServer.Refresh(); }