We have an application that uses a SQLDataReader to cycle through a control
table in the database and process records based upon that data. These
records include things like the directory to poll and where to move the
data.
During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.ExecuteNonQuery function to execute the stored procedure. If this
operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open reader
on the connection object.
My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.ExecuteReader
loop through the reader
close the connection
and have a different connection object for the SQL Statement as opposed to
using the global connection object that the SQLCommand (above) is using?
2. As I am executing a NonQuery why would that conflict with an open reader?
Nothing is being returned relative to a Reader object.
3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?
4. Is the best practice to open and close connections consistantly? In older
versions of VB we found that opening a connection to the same database was
very resource intensive which is why the original code (prior to porting to
..NET) never closed the database connection. Has that been addressed with the
SQLClient object?
Thanks in advance.