1

这是我用来恢复数据库的代码

SqlConnection c = new SqlConnection("server=.\\SQLExpress;Persist Security Info= True;integrated security=True;Trusted_Connection=Yes");
                c.Open();
//Setting database into single user mode first
                SqlCommand cd = new SqlCommand("ALTER DATABASE Society_security_database SET SINGLE_USER WITH ROLLBACK IMMEDIATE", c);
                cd.ExecuteNonQuery();
//Restoring database from a location
                cd = new SqlCommand("RESTORE DATABASE Society_security_database FROM DISK = '" + path + "' WITH REPLACE", c);
                cd.ExecuteNonQuery();
//Finally setting it to MultiUSermode again
                cd = new SqlCommand("ALTER DATABASE Society_security_database SET MULTI_USER", c);
                cd.ExecuteNonQuery();
                SqlConnection.ClearAllPools();
                c.Close();

但是当恢复失败时,第三次查询没有执行,程序异常中止,如何解决这个问题?

4

0 回答 0