Detect there is already an open OleDbDataReader associated with OleDbCommand
Is It possible to detect there is already an open OleDbDataReader associated with OleDbCommand?
Re: Detect there is already an open OleDbDataReader associated with OleDbCommand
If there's a DataReader open it's because you called ExecuteReader on a Command so you should know already. Also, once ExecuteReader returns there is no longer any association between the Command and the DataReader. There is an association between the DataReader and the Connection, because the Connection is used to retrieve the data contained in the DataReader's result set. You could check out the State property of the Connection but I don't think it will tell you nay more than Open or Closed at this point.
Re: Detect there is already an open OleDbDataReader associated with OleDbCommand
jmcilhinney,
I know if I call ExecuteReader the DataReader is executing, the question is different.
I want to "share" the Command object and I "send" this Command to many instances and with 1 Command I execute many operations in many instances.
If I use a DataReader in an instance and I use the same Command in other instance, I want to know If this Command has a DataReader associated, but the Command object doesn't have any Property that inform this.
Do you understand me?
Re: Detect there is already an open OleDbDataReader associated with OleDbCommand
As I have already said, Commands and DataReaders are not associated. You call the ExecuteReader method and it returns a DataReader. Once ExecuteReader returns the DataReader is associated with a result set in a database via a Connection. It has no idea what Command was executed to create that result set, nor does the Command know anything more about the DataReader it created. The two don't need to know anything about each other.
You can call ExecuteQuery on the same Command a thousand times and have a thousand open DataReaders and it makes no difference to the Command. It doesn't know or care. It's job is to execute an SQL statement and create a DataReader to access the result set. Once that's done the Command washes its hands of any further responsibility.
If you want to maintain some association between the DataReader and the Command that created it then it's up to you to do it because ADO.NET doesn't. The reason it doesn't is that there isn't really a need to. The DataReader needs the Connection exclusively. That's where there's an association.