我正在使用此函数恢复SQL Server数据库。
它工作得很好,但我的问题是,当我使用此函数时,我必须确保在我的应用程序中关闭了SQL Server连接。
如果在其他窗口中打开了SQL Server连接,则此功能将不起作用。
那么,即使SQL Server连接在另一个窗口中打开,如何恢复我的数据库呢?
我的意思是,在我的还原功能期间,有没有办法锁定SQL Server?
private void btnRestore_Click(object sender, EventArgs e)
{
// If there was a SQL connection created
try
{
if (srvSql != null)
{
saveBackupDialog.Title = "Restore Backup File";
saveBackupDialog.InitialDirectory = "D:";
// If the user has chosen the file from which he wants the database to be restored
if (openFD.ShowDialog() == DialogResult.OK)
{
Thread oThread = new Thread(new ThreadStart(frmWaitShow));
oThread.Start();
// Create a new database restore operation
Restore rstDatabase = new Restore();
// Set the restore type to a database restore
rstDatabase.Action = RestoreActionType.Database;
// Set the database that we want to perform the restore on
rstDatabase.Database = cmbDatabase.SelectedItem.ToString();
// Set the backup device from which we want to restore, to a file
BackupDeviceItem bkpDevice = new BackupDeviceItem(openFD.FileName, DeviceType.File);
// Add the backup device to the restore type
rstDatabase.Devices.Add(bkpDevice);
// If the database already exists, replace it
rstDatabase.ReplaceDatabase = true;
// Perform the restore
rstDatabase.SqlRestore(srvSql);
oThread.Suspend();
MessageBox.Show("DataBase Restore Successfull");
}
else
{
// There was no connection established; probably the Connect button was not clicked
MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}发布于 2013-12-14 14:26:54
我发现杀死数据库的所有进程,设置单用户模式,然后分离数据库是有效的,并且可以使用SMO来完成。为了涵盖不同的场景,所有这三个步骤都是必要的,尽管我不能马上告诉你它们是什么。从理论上讲,只需要将数据库置于单用户模式。
// Kill all processes
sqlServer.KillAllProcesses(restore.Database);
// Set single-user mode
Database db = sqlServer.Databases[restore.Database];
db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
db.Alter(TerminationClause.RollbackTransactionsImmediately);
// Detach database
sqlServer.DetachDatabase(restore.Database, false);在你的方法中:
private void btnRestore_Click(object sender, EventArgs e)
{
// If there was a SQL connection created
try
{
if (srvSql != null)
{
saveBackupDialog.Title = "Restore Backup File";
saveBackupDialog.InitialDirectory = "D:";
// If the user has chosen the file from which he wants the database to be restored
if (openFD.ShowDialog() == DialogResult.OK)
{
Thread oThread = new Thread(new ThreadStart(frmWaitShow));
oThread.Start();
// Create a new database restore operation
Restore rstDatabase = new Restore();
// Set the restore type to a database restore
rstDatabase.Action = RestoreActionType.Database;
// Set the database that we want to perform the restore on
rstDatabase.Database = cmbDatabase.SelectedItem.ToString();
// Set the backup device from which we want to restore, to a file
BackupDeviceItem bkpDevice = new BackupDeviceItem(openFD.FileName, DeviceType.File);
// Add the backup device to the restore type
rstDatabase.Devices.Add(bkpDevice);
// If the database already exists, replace it
rstDatabase.ReplaceDatabase = true;
// Kill all processes
srvSql.KillAllProcesses(rstDatabase.Database);
// Set single-user mode
Database db = srvSql.Databases[rstDatabase.Database];
db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
db.Alter(TerminationClause.RollbackTransactionsImmediately);
// Detach database
srvSql.DetachDatabase(rstDatabase.Database, false);
// Perform the restore
rstDatabase.SqlRestore(srvSql);
oThread.Suspend();
MessageBox.Show("DataBase Restore Successfull");
}
else
{
// There was no connection established; probably the Connect button was not clicked
MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}https://stackoverflow.com/questions/20580276
复制相似问题