使用这个我成功地备份了我的数据库:
Server myServer = new Server(@"PC-1\INSTANCE1");
private void button1_Click(object sender, EventArgs e)
{
try
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Backup bkpDBFull = new Backup();
bkpDBFull.Action = BackupActionType.Database;
bkpDBFull.Database = "MyDatabase";
BackupDeviceItem bkpDevice = new BackupDeviceItem(saveFileDialog1.FileName, DeviceType.File);
bkpDBFull.Devices.Add(bkpDevice);
bkpDBFull.BackupSetName = "Sample";
bkpDBFull.BackupSetDescription = "Sample";
bkpDBFull.SqlBackup(myServer);
}
}
catch (SmoException ex)
{
throw new SmoException(ex.Message, ex.InnerException);
}
}
但是我的还原出错了Restore failed for Server 'PC-1\INSTANCE1'.
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Restore restoreDB = new Restore();
restoreDB.Database = "MyDatabase";
restoreDB.Action = RestoreActionType.Database;
BackupDeviceItem bkpDevice = new BackupDeviceItem(openFileDialog1.FileName, DeviceType.File);
restoreDB.Devices.Add(bkpDevice);
restoreDB.ReplaceDatabase = true;
restoreDB.SqlRestore(myServer);
}
}
我在备份和还原中使用同一台服务器。为什么使用相同的服务器和数据库备份成功但恢复失败?