如下代码用在数据库还原中,用于杀死连接数据库的进程,问题是:数据库还原成功后无法继续对数据库操作,除非重启客户端,代码如下:
private void KillSPID(string DBName)
{
string strDBName = DBName;
string strSQL = String.Empty, strSQLKill = String.Empty;
//因为DataReader独占连接,本程序需要两个Connection,并且连接的数据库是master,所以要初始化新的连接字符串
string tmpConnectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=master;"; try
{
SqlConnection TmpConn = new SqlConnection();
SqlConnection conn = new SqlConnection();
TmpConn.ConnectionString = tmpConnectionString;
conn.ConnectionString = tmpConnectionString;
TmpConn.Open();
conn.Open(); //读取连接当前数据库的进程
strSQL = "select spid from master..sysprocesses where dbid=db_id('" + strDBName + "')";
SqlCommand mycmd = new SqlCommand(strSQL, TmpConn);
SqlDataReader mydr = mycmd.ExecuteReader(); //开取杀进程的数据连接
SqlCommand mycmd1 = new SqlCommand();
mycmd1.Connection = conn; while (mydr.Read())
{
strSQLKill = "kill " + mydr["spid"].ToString();
mycmd1.CommandText = strSQLKill;
mycmd1.CommandType = CommandType.Text;
mycmd1.ExecuteNonQuery(); //杀进程
}
mydr.Close();
TmpConn.Close();
conn.Close();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
有什么方法,使客户端不用重启,就能继续操作数据库吗??
private void KillSPID(string DBName)
{
string strDBName = DBName;
string strSQL = String.Empty, strSQLKill = String.Empty;
//因为DataReader独占连接,本程序需要两个Connection,并且连接的数据库是master,所以要初始化新的连接字符串
string tmpConnectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=master;"; try
{
SqlConnection TmpConn = new SqlConnection();
SqlConnection conn = new SqlConnection();
TmpConn.ConnectionString = tmpConnectionString;
conn.ConnectionString = tmpConnectionString;
TmpConn.Open();
conn.Open(); //读取连接当前数据库的进程
strSQL = "select spid from master..sysprocesses where dbid=db_id('" + strDBName + "')";
SqlCommand mycmd = new SqlCommand(strSQL, TmpConn);
SqlDataReader mydr = mycmd.ExecuteReader(); //开取杀进程的数据连接
SqlCommand mycmd1 = new SqlCommand();
mycmd1.Connection = conn; while (mydr.Read())
{
strSQLKill = "kill " + mydr["spid"].ToString();
mycmd1.CommandText = strSQLKill;
mycmd1.CommandType = CommandType.Text;
mycmd1.ExecuteNonQuery(); //杀进程
}
mydr.Close();
TmpConn.Close();
conn.Close();
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
有什么方法,使客户端不用重启,就能继续操作数据库吗??
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货