我正在使用 C# 代码还原 SQL 数据库。当我们使用 SQL Server Management Studio 还原备份时,我们可以选择备份集。我们可以使用代码来执行此操作吗?
我使用了以下代码
openFileDialog1.ShowDialog();
string databaseName = "TempDb";
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(openFileDialog1.FileName, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
DataConnection dataConnection = new DataConnection();
ServerConnection connection = new ServerConnection(dataConnection.DataBaseConnection);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
sqlRestore.Action = RestoreActionType.Database;
String dataFileLocation = db.FileGroups[0].Files[0].FileName;
String logFileLocation = db.LogFiles[0].FileName;
db = sqlServer.Databases[databaseName];
sqlServer.ConnectionContext.Disconnect();
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();
使用上面的代码它只恢复第一组。