读书人

恢复数据库解决方法

发布时间: 2012-01-10 21:26:50 作者: rapoo

恢复数据库
使用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

读书人网 >asp.net

热点推荐