私は以下のDataHelperClassを持つmvc 4ウェブサイトでクエリを実行しています。私の問題は時々、ウェブサイトからタイトルとしてのウェブサイトです。私は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'が問題です。 –