I have a memory leak, due to not closing the connection properly. This is due to using a global function to access the database (with different sql strings), but I pass back an sqldatareader. I cant close this in the method, nor the connection to the DB, as it closes access to the data! And it doesnt close properly from outside this method. :(
Is there Anyway way I can take the desired table, that the sqldatareader grants access to, offline. So that I can close all the connections, but still access the table.
Note, Different tables are returned so different fields exist. I dont want to have to duplicate code each time I try and connect.
private SqlDataReader OpenDataStream(String sql)
{
SqlCommand sqlComm = new SqlCommand();
sqlComm.Connection = new SqlConnection();
sqlComm.Connection.ConnectionString = @"Myconnectionstring";
sqlComm.CommandText = sql;
sqlComm.Connection.Open();
SqlDataReader data = null;
data = sqlComm.ExecuteReader();
return data;
// Closing data here, or connection, results in returned object inaccessable.
}
or maybe a valid working way of closing it all down outside the method (after I have accessed what I need)?