Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#还原SQL Server 2005数据库。回滚问题_C#_Sql Server 2005_Database Restore - Fatal编程技术网

使用C#还原SQL Server 2005数据库。回滚问题

使用C#还原SQL Server 2005数据库。回滚问题,c#,sql-server-2005,database-restore,C#,Sql Server 2005,Database Restore,嗨,我刚开始学C。我正在尝试还原.bak文件。然而,我得到了错误。无法获得独占访问权限,因为数据库正在使用中 我做了研究,两人都说我必须执行回滚。我不知道如何在还原代码中应用回滚 public void RestoreDatabase(String RestorePath) { try { SqlConnection sqlCon = new SqlConnection("Data Source=RITZEL-PC\\SQL

嗨,我刚开始学C。我正在尝试还原.bak文件。然而,我得到了错误。无法获得独占访问权限,因为数据库正在使用中

我做了研究,两人都说我必须执行回滚。我不知道如何在还原代码中应用回滚

    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);
        }
    }
这行吗

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

使用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();

我要把它放在哪里?还原前还原b=new Restore();?是的,它应该终止与数据库的所有连接关键字“SET”和“with”附近不正确的语法。如果此语句是公共表表达式或xmlnamespaces子句,则前面的语句必须以分号终止。“IMMEDIATE”附近的语法不正确请确保引用的是多工作数据库名称,例如带方括号的[NNIT DB]