I have a base class that contains a SqlDataReader
which gets the data using a stored procedure, this works great until I return the data reader back up saying the connection is null.
Does anyone have any ideas? here's my code:
public SqlDataReader GetDataReader(string QueryName, SqlParameter[] Params)
{
SqlConnection conn = new SqlConnection(this.ConnectionString);
SqlDataReader reader;
using (conn)
{
SqlCommand command = new SqlCommand(QueryName,conn);
command.CommandType = CommandType.StoredProcedure;
if(Params !=null)
command.Parameters.Add(Params);
conn.Open();
reader = command.ExecuteReader();
}
// conn.Close();
return reader;
}
If you notice, I have the close part commented out, this was me trying to get it to work, for some reason when returning the datareader back up it is set to close???
Thanks!
SqlDataReader
- that's a horrible design and just a recipe for disaster. Instead, have your method actually read the data, put it into aList<Something>
, close yourSqlDataReader
again as quickly as possible, and return that list back to the caller... – marc_s Nov 14 '11 at 9:16