473,503 Members | 12,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataReader and ExecuteNonQuery

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.
Nov 21 '05 #1
8 5093
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hotmail.com> schreef in bericht
news:Ov**************@TK2MSFTNGP10.phx.gbl...
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.

Nov 21 '05 #2
Stephen Costanzo wrote:
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.


1. Open Connection -> Execute Reader -> Loop Reader -> Close Reader
You don't need to close the connection, just close the reader. Then you
will be able to do other calls on the connection.

2. The reader is tied to the connection and has the connection locked,
that's where the conflict is.

3. This is by design. A reader doesn't bring all the data down at one
time, only every time do you the reader.read() so the connection must
remain open (and tied up for other calls) to keep the place you are in
your reader.

4. I wouldn't open and close consistantly.

Chris
Nov 21 '05 #3
Thanks for the help.

"Chris" <no@spam.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...
Stephen Costanzo wrote:
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.


1. Open Connection -> Execute Reader -> Loop Reader -> Close Reader
You don't need to close the connection, just close the reader. Then you
will be able to do other calls on the connection.

2. The reader is tied to the connection and has the connection locked,
that's where the conflict is.

3. This is by design. A reader doesn't bring all the data down at one
time, only every time do you the reader.read() so the connection must
remain open (and tied up for other calls) to keep the place you are in
your reader.

4. I wouldn't open and close consistantly.

Chris

Nov 21 '05 #4
So in Net 2.x will there be the ability to have more than one open
connection at the same time?

Do you know if there is something on the microsoft site? i tried several
keywords and was not getting meaningful results.

Thanks

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:u8**************@TK2MSFTNGP12.phx.gbl...
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hotmail.com> schreef in bericht
news:Ov**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 21 '05 #5
Stephen,

Probably is this the information you are looking for.
Have a look at Mars(datareaders) and connection pooling.

http://msdn2.microsoft.com/en-us/library/ex6y04yf

I hope this helps

Cor

"Stephen Costanzo" <sx********@hotmail.com> schreef in bericht
news:ef**************@TK2MSFTNGP09.phx.gbl...
So in Net 2.x will there be the ability to have more than one open
connection at the same time?

Do you know if there is something on the microsoft site? i tried several
keywords and was not getting meaningful results.

Thanks

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:u8**************@TK2MSFTNGP12.phx.gbl...
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hotmail.com> schreef in bericht
news:Ov**************@TK2MSFTNGP10.phx.gbl...
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.



Nov 21 '05 #6
very cool.

Thanks

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ek****************@TK2MSFTNGP14.phx.gbl...
Stephen,

Probably is this the information you are looking for.
Have a look at Mars(datareaders) and connection pooling.

http://msdn2.microsoft.com/en-us/library/ex6y04yf

I hope this helps

Cor

"Stephen Costanzo" <sx********@hotmail.com> schreef in bericht
news:ef**************@TK2MSFTNGP09.phx.gbl...
So in Net 2.x will there be the ability to have more than one open
connection at the same time?

Do you know if there is something on the microsoft site? i tried several
keywords and was not getting meaningful results.

Thanks

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:u8**************@TK2MSFTNGP12.phx.gbl...
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hotmail.com> schreef in bericht
news:Ov**************@TK2MSFTNGP10.phx.gbl...
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.



Nov 21 '05 #7
Cor,

What are you talking about here? You can have as many open connections as
you want.

Perhaps you meant that you can only have one open data reader per connection?

Kerry Moorman
"Cor Ligthert [MVP]" wrote:
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor


Nov 21 '05 #8
Stephen,

Using one open global connection is a design mistake in .Net.

You need to open and close connections each time you access the database.

Kerry Moorman
"Stephen Costanzo" wrote:
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.

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3046
by: Maziar Aflatoun | last post by:
Hi everyone, I am working on the 'Delete' section of my program. What I need to do is query my database and for every ID that it finds I want to remove a file+ID.jpg from my file folder and...
4
3496
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view...
3
9416
by: RSH | last post by:
Hi, I have a situation where I have two datareaders, and I want to make sure any given field from Datareader A exists in Datareader B before I can do anything with that column. I tried the code...
20
7177
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
13
3397
by: Bart | last post by:
Hi, i get the error: "There is already an open DataReader associated with this Command which must be closed first" Thanks Bart ----------------------------------------- Imports...
0
2872
by: Teo | last post by:
Hi!! I have been trying to fix an error I keep getting on my VB.NET code. For some reason, I get an error saying "Invalid attempt to access a field before calling Read()" everytime. As you can see...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.