Forum Moderators: open
Because it's a DataReader you can't close the connection even if you attempt to close it in a try,catch finally block. The client waill gak becuase it needs the connection to be open to uncork the datareader.
The key to shutting down the connection is to make sure that the CommandBehavior gets passed into the Execute Reader method (see code below). The same can be used with the OleDbDataReader too.
I just chased this down a few weeks ago and until I passed CommandBehavior.CloseConnection into ExecuteReader one of my applications was leaving connections open on the database and they would grow throughout the day.
I just scratched this out so now warranty but it demonstrates 'using' :)
SqlDataReader r;
using(SqlConnection mCNN = new SqlConnection("yourconnstring"))
{
using(SqlCommand mCMD = new SqlCommand("sprocName", mCNN))
{
mCmd.CommandType.CommandType = CommandType.StoredProcedure;
try
{
mCMD.Open();
r = mCMD.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception se)
{
// Do something with this error
se.ToString();
}
}
}