Is it possible to know if a connection is being used by a datareader?

  • Thread starter Thread starter Lu Jimmy
  • Start date Start date
L

Lu Jimmy

Is it possible to know if a connection is being used by a DataReader(or
others)? So I can run the ExecuteReader command without error. Thanks

Jimmy
 
You might check IDbConnection.State property.
But I don't see any great need for that - don't you know what your code is
doing? It should be apparent from the code what the connection is doing.
 
The IDbConnection.State property can only shows the opened/closed status of
the connection.

I have some code as follow:

SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read()) {
SqlDataReader dr = cmd2.ExecuteReader(); // InvalidOperationException
......
}

Sometimes I need to run the code:
cmd.ExecuteReader()
inside the
DataAdapter.Fill(dataTable)
operation, it still comes the same Exception.

I want to check the connection before the exception comes, How should I do
that?

Jimmy



Miha Markic said:
You might check IDbConnection.State property.
But I don't see any great need for that - don't you know what your code is
doing? It should be apparent from the code what the connection is doing.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Lu Jimmy said:
Is it possible to know if a connection is being used by a DataReader(or
others)? So I can run the ExecuteReader command without error. Thanks

Jimmy
 
Hi Jimmy,

Jimmy Lu said:
The IDbConnection.State property can only shows the opened/closed status
of the connection.

Ah, right Executing and Fetching values are for future versions of the
product. I wonder why are not already implemented.
I have some code as follow:

SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read()) {
SqlDataReader dr = cmd2.ExecuteReader(); // InvalidOperationException
......
}

Sometimes I need to run the code:
cmd.ExecuteReader()
inside the
DataAdapter.Fill(dataTable)
operation, it still comes the same Exception.

I want to check the connection before the exception comes, How should I do
that?
You don't need to check - isn't it obvious that you need another connection?
You should create another Connection instance in such situations.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
Jimmy



Miha Markic said:
You might check IDbConnection.State property.
But I don't see any great need for that - don't you know what your code
is doing? It should be apparent from the code what the connection is
doing.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Lu Jimmy said:
Is it possible to know if a connection is being used by a DataReader(or
others)? So I can run the ExecuteReader command without error. Thanks

Jimmy
 
Hi Jimmy,



Ah, right Executing and Fetching values are for future versions of the
product. I wonder why are not already implemented.

You don't need to check - isn't it obvious that you need another connection?
You should create another Connection instance in such situations.

Miha,

I ran into Jimmy's situation once when using transactions on the same connection
that spanned multiple method calls, so I see the validity of Jimmy's question

The idea was that the methods would be called at times without a transaction, so
it was necessary to check for an already open connection before opening a
connection for the database operation. I checked the Connection.State property
to determine if the connection was open. It worked just fine.

So Jimmy, my answer to your question would be that you can check the
Connection.State property of the connection to determine if the connection is
open, before attempting to open it again.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Back
Top