使用 C# 恢复 SQL Server 2005 数据库。回滚问题

发布于 2025-01-06 12:33:37 字数 1379 浏览 0 评论 0原文

大家好,我刚刚开始学习 C#。我正在尝试恢复 .bak 文件。但是我收到了错误。由于数据库正在使用中,无法获得独占访问权限。

我在这里进行了研究< /a> 和 这里都说我必须执行回滚。我不知道如何在我的恢复代码中应用回滚。

    public void RestoreDatabase(String RestorePath)
    {
        try
        {
            SqlConnection sqlCon = new SqlConnection("Data Source=RITZEL-PC\\SQLEXPRESS;User ID=NNIT-Admin;Password=password;Initial Catalog=master;");
            ServerConnection connection = new ServerConnection(sqlCon);
            Server sqlServer = new Server(connection);

            Restore restoreDB = new Restore();

            restoreDB.Database = "NNIT DB";
            restoreDB.Action = RestoreActionType.Database;
            restoreDB.Devices.AddDevice(RestorePath, DeviceType.File);

            restoreDB.ReplaceDatabase = true; // will overwrite any existing DB     
            restoreDB.NoRecovery = false; // NoRecovery = true;

            restoreDB.SqlRestore(sqlServer);

            MessageBox.Show("Restored");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + " " + ex.InnerException);
        }
    }

Hi I'm just starting to learn C#. I am trying to restore a .bak file. However I am getting the error. Exclusive access cannot be obtained because the database is in use.

I did my research here and here both says I have to perform a rollback. I do not know how to apply rollback in my restore code.

    public void RestoreDatabase(String RestorePath)
    {
        try
        {
            SqlConnection sqlCon = new SqlConnection("Data Source=RITZEL-PC\\SQLEXPRESS;User ID=NNIT-Admin;Password=password;Initial Catalog=master;");
            ServerConnection connection = new ServerConnection(sqlCon);
            Server sqlServer = new Server(connection);

            Restore restoreDB = new Restore();

            restoreDB.Database = "NNIT DB";
            restoreDB.Action = RestoreActionType.Database;
            restoreDB.Devices.AddDevice(RestorePath, DeviceType.File);

            restoreDB.ReplaceDatabase = true; // will overwrite any existing DB     
            restoreDB.NoRecovery = false; // NoRecovery = true;

            restoreDB.SqlRestore(sqlServer);

            MessageBox.Show("Restored");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + " " + ex.InnerException);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2025-01-13 12:33:37

使用 SMO,您可以像这样设置用户访问和回滚:

Server sqlServer = new Server(connection);

Database db = sqlServer.Databases["DbToRestore"];

if (db != null)
{
    sqlServer.KillAllProcesses(db.Name);
    db.DatabaseOptions.UserAccess = DatabaseUserAccess.Multiple;
    db.Alter(TerminationClause.RollbackTransactionsImmediately);
}

Restore restoreDB = new Restore();

Using SMO you can set user access and rollback like this:

Server sqlServer = new Server(connection);

Database db = sqlServer.Databases["DbToRestore"];

if (db != null)
{
    sqlServer.KillAllProcesses(db.Name);
    db.DatabaseOptions.UserAccess = DatabaseUserAccess.Multiple;
    db.Alter(TerminationClause.RollbackTransactionsImmediately);
}

Restore restoreDB = new Restore();
飘落散花 2025-01-13 12:33:37

这有效吗?

SqlCommand cmd = new SqlCommand("ALTER DATABASE yourdatabasename SET MULTI_USER WITH ROLLBACK IMMEDIATE", sqlConn);
cmd.ExecuteNonQuery();

Does this work?

SqlCommand cmd = new SqlCommand("ALTER DATABASE yourdatabasename SET MULTI_USER WITH ROLLBACK IMMEDIATE", sqlConn);
cmd.ExecuteNonQuery();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文