我有mvc 4网站与DataHelperClass下面执行查询。我的问题是,有时候,网站通过作为标题的怀疑。我用block来处理SqlCommand和SqlDataAdapter,但没有成功。错误:ExecuteReader需要一个开放且可用的连接。该连接的当前状态是打开的
请帮助我,对不起我的英语。
try
{
if (_conn.State == ConnectionState.Closed)
_conn.Open();
using (SqlCommand sqlCommand = new SqlCommand(query, _conn))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
if (parameters != null)
sqlCommand.Parameters.AddRange(parameters);
//// check transaction is exist
if (_trans != null)
sqlCommand.Transaction = _trans;
DataTable dt = new DataTable();
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
sqlDataAdapter.Fill(dt);
}
return dt;
}
}
finally
{
//// close connection automatically if transaction is not exist
if (_trans == null) { _conn.Close(); }
}
最有可能的'静态的SqlConnection _conn',你没有在样品中显示的问题。 –