?
原SqlHelper类中ExecuteReader方法体为:
public static SqlDataReader ExecuteReader(string sqlString, params SqlParameter[] sqlParams) { using (SqlConnection conn = new SqlConnection(connString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(conn, cmd, sqlString, sqlParams); SqlDataReader rdr = cmd.ExecuteReader(); cmd.Parameters.Clear(); return rdr; } catch { return null; } finally { conn.Close(); conn.Dispose(); cmd.Dispose(); } } } }
出现该错误的原因是using的问题,而且finally会在自动关闭conn,而不管reader是否传递。
将ExecuteReader方法体修改为:
public static SqlDataReader ExecuteReader(string sqlString, params SqlParameter[] sqlParams) { SqlConnection conn = new SqlConnection(ConnString); SqlCommand cmd = new SqlCommand(); try { PrepareCommand(conn, cmd, sqlString, sqlParams); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch { return null; } }
?
?