日期:2014-05-19  浏览次数:20437 次

恢复数据库
使用restore恢复数据库时,报错:
RESTORE   DATABASE   操作异常终止。
因为数据库正在使用,所以未能获得对数据库的排它访问权。


是用kill语句关闭进程吗?怎么用啊?

------解决方案--------------------
用这个
#region 关闭所有使用中的进程
public void KillDataBaseProcesses(string strDataBaseName)
{
SqlConnection conn = new SqlConnection(ConnectString);

string strSql = "select spid from master..sysprocesses where dbid=db_id( ' " + strDataBaseName + " ') ";

SqlCommand comm = new SqlCommand(strSql, conn);

try
{
conn.Open();

SqlDataReader dr = comm.ExecuteReader();

while (dr.Read())
{
strSql = "kill " + (dr[0].ToString());

comm = new SqlCommand(strSql, conn);
comm.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
{
conn.Dispose();
}
}
}
}
#endregion