首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用C#恢复正在使用的SQL Server数据库

如何使用C#恢复正在使用的SQL Server数据库
EN

Stack Overflow用户
提问于 2013-12-14 14:10:58
回答 3查看 11.1K关注 0票数 7

我正在使用此函数恢复SQL Server数据库。

它工作得很好,但我的问题是,当我使用此函数时,我必须确保在我的应用程序中关闭了SQL Server连接。

如果在其他窗口中打开了SQL Server连接,则此功能将不起作用。

那么,即使SQL Server连接在另一个窗口中打开,如何恢复我的数据库呢?

我的意思是,在我的还原功能期间,有没有办法锁定SQL Server?

代码语言:javascript
运行
复制
    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);
                }
            }
        }
EN

Stack Overflow用户

回答已采纳

发布于 2013-12-14 14:26:54

我发现杀死数据库的所有进程,设置单用户模式,然后分离数据库是有效的,并且可以使用SMO来完成。为了涵盖不同的场景,所有这三个步骤都是必要的,尽管我不能马上告诉你它们是什么。从理论上讲,只需要将数据库置于单用户模式。

代码语言:javascript
运行
复制
// 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);

在你的方法中:

代码语言:javascript
运行
复制
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);
            }

        }
    }
票数 13
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20580276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档