473,804 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Several Recordsets...Cl ose the Connection Each Time?

Bob
I need to get several recordset for which I'm opening a datareader
like so...

OleDbCommand rsA = new OleDbCommand("S elect * from Authors",cnAcce ss);
OleDbDataReader drA = rsA.ExecuteRead er();
while(drA.Read( ))
{
sbL.Append("<tr ><td>"+drA["Name"].ToString()+"</td></tr>");
}

This works fine......but when I try for the second recordset like
this....

OleDbCommand rsT = new OleDbCommand("S elect * fromTitles",cnA ccess);
OleDbDataReader drT = rsT.ExecuteRead er();
while(drT.Read( ))
{
sbL.Append("<tr ><td>"+drT["Name"].ToString()+"</td></tr>");
}

I get an error...I "fixed" it by closing...then re-opening the
connection....b ut do I need to???

Why can't I open the connection and re-use the connection over and
over until I have everything..... ..as a matter of fact....I should be
able to re-use the dr also....

Anybody know?

Bob Sweeney

Feb 22 '07 #1
3 1699
Hi,
What you are trying to do is possible in ADO.NET 2.0 with a feature called
MARS(Multiple Active Result Sets).Search in any serach engine for MARS and
you will get plenty of articles on it.

Thanks and regards,
Manish Bafna.
MCP and MCTS.

"Bob" wrote:
I need to get several recordset for which I'm opening a datareader
like so...

OleDbCommand rsA = new OleDbCommand("S elect * from Authors",cnAcce ss);
OleDbDataReader drA = rsA.ExecuteRead er();
while(drA.Read( ))
{
sbL.Append("<tr ><td>"+drA["Name"].ToString()+"</td></tr>");
}

This works fine......but when I try for the second recordset like
this....

OleDbCommand rsT = new OleDbCommand("S elect * fromTitles",cnA ccess);
OleDbDataReader drT = rsT.ExecuteRead er();
while(drT.Read( ))
{
sbL.Append("<tr ><td>"+drT["Name"].ToString()+"</td></tr>");
}

I get an error...I "fixed" it by closing...then re-opening the
connection....b ut do I need to???

Why can't I open the connection and re-use the connection over and
over until I have everything..... ..as a matter of fact....I should be
able to re-use the dr also....

Anybody know?

Bob Sweeney

Feb 22 '07 #2
Hi there,

I reckon MARS is only supported by SQL Server 2005 / Oracle (and don't think
one would use OleDbProvider for SQL Server2005 - or based on his code he's
definitely ASP guy :D, and Oracle natively supports MARS so he probably would
not get this exception) Therefore Bob, you just have to close associated
reader before executing next query / stored procedure

OleDbCommand rsA = new OleDbCommand("S elect * from Authors",cnAcce ss);
OleDbDataReader drA = rsA.ExecuteRead er();
while(drA.Read( ))
{
sbL.Append("<tr ><td>"+drA["Name"].ToString()+"</td></tr>");
}

// close reader before executing next statement
drA.Close()

OleDbCommand rsT = new OleDbCommand("S elect * fromTitles",cnA ccess);
OleDbDataReader drT = rsT.ExecuteRead er();
while(drT.Read( ))
{
sbL.Append("<tr ><td>"+drT["Name"].ToString()+"</td></tr>");
}

Additional resolution would be to execute statements in one batch and move
to next result set:

OleDbCommand rsA = new OleDbCommand(
"Select * from Authors; GO; " +
"Select * fromTitles; GO;",cnAccess) ;
OleDbDataReader dr = rsA.ExecuteRead er();

while(dr.Read() )
{
sbL.AppendForma t("<tr><td>{0 }</td></tr>", drA["Name"].ToString());
}

dr.NextResult() ;

while(dr.Read() )
{
sbL.AppendForma t("<tr><td>{0 }</td></tr>", drT["Name"].ToString());
}

hope this helps
--
Milosz
"Manish Bafna" wrote:
Hi,
What you are trying to do is possible in ADO.NET 2.0 with a feature called
MARS(Multiple Active Result Sets).Search in any serach engine for MARS and
you will get plenty of articles on it.

Thanks and regards,
Manish Bafna.
MCP and MCTS.

"Bob" wrote:
I need to get several recordset for which I'm opening a datareader
like so...

OleDbCommand rsA = new OleDbCommand("S elect * from Authors",cnAcce ss);
OleDbDataReader drA = rsA.ExecuteRead er();
while(drA.Read( ))
{
sbL.Append("<tr ><td>"+drA["Name"].ToString()+"</td></tr>");
}

This works fine......but when I try for the second recordset like
this....

OleDbCommand rsT = new OleDbCommand("S elect * fromTitles",cnA ccess);
OleDbDataReader drT = rsT.ExecuteRead er();
while(drT.Read( ))
{
sbL.Append("<tr ><td>"+drT["Name"].ToString()+"</td></tr>");
}

I get an error...I "fixed" it by closing...then re-opening the
connection....b ut do I need to???

Why can't I open the connection and re-use the connection over and
over until I have everything..... ..as a matter of fact....I should be
able to re-use the dr also....

Anybody know?

Bob Sweeney
Feb 22 '07 #3
Forgot to change "drA" / "drT" to "dr" in second snippet, should be:

OleDbCommand rsA = new OleDbCommand(
"Select * from Authors; GO; " +
"Select * fromTitles; GO;",cnAccess) ;
OleDbDataReader dr = rsA.ExecuteRead er();

while(dr.Read() )
{
sbL.AppendForma t("<tr><td>{0 }</td></tr>", dr["Name"].ToString());
}

dr.NextResult() ;

while(dr.Read() )
{
sbL.AppendForma t("<tr><td>{0 }</td></tr>", dr["Name"].ToString());
}

Regards
--
Milosz
"Milosz Skalecki [MCAD]" wrote:
Hi there,

I reckon MARS is only supported by SQL Server 2005 / Oracle (and don't think
one would use OleDbProvider for SQL Server2005 - or based on his code he's
definitely ASP guy :D, and Oracle natively supports MARS so he probably would
not get this exception) Therefore Bob, you just have to close associated
reader before executing next query / stored procedure

OleDbCommand rsA = new OleDbCommand("S elect * from Authors",cnAcce ss);
OleDbDataReader drA = rsA.ExecuteRead er();
while(drA.Read( ))
{
sbL.Append("<tr ><td>"+drA["Name"].ToString()+"</td></tr>");
}

// close reader before executing next statement
drA.Close()

OleDbCommand rsT = new OleDbCommand("S elect * fromTitles",cnA ccess);
OleDbDataReader drT = rsT.ExecuteRead er();
while(drT.Read( ))
{
sbL.Append("<tr ><td>"+drT["Name"].ToString()+"</td></tr>");
}

Additional resolution would be to execute statements in one batch and move
to next result set:

OleDbCommand rsA = new OleDbCommand(
"Select * from Authors; GO; " +
"Select * fromTitles; GO;",cnAccess) ;
OleDbDataReader dr = rsA.ExecuteRead er();

while(dr.Read() )
{
sbL.AppendForma t("<tr><td>{0 }</td></tr>", drA["Name"].ToString());
}

dr.NextResult() ;

while(dr.Read() )
{
sbL.AppendForma t("<tr><td>{0 }</td></tr>", drT["Name"].ToString());
}

hope this helps
--
Milosz
"Manish Bafna" wrote:
Hi,
What you are trying to do is possible in ADO.NET 2.0 with a feature called
MARS(Multiple Active Result Sets).Search in any serach engine for MARS and
you will get plenty of articles on it.

Thanks and regards,
Manish Bafna.
MCP and MCTS.

"Bob" wrote:
I need to get several recordset for which I'm opening a datareader
like so...
>
OleDbCommand rsA = new OleDbCommand("S elect * from Authors",cnAcce ss);
OleDbDataReader drA = rsA.ExecuteRead er();
while(drA.Read( ))
{
sbL.Append("<tr ><td>"+drA["Name"].ToString()+"</td></tr>");
}
>
This works fine......but when I try for the second recordset like
this....
>
OleDbCommand rsT = new OleDbCommand("S elect * fromTitles",cnA ccess);
OleDbDataReader drT = rsT.ExecuteRead er();
while(drT.Read( ))
{
sbL.Append("<tr ><td>"+drT["Name"].ToString()+"</td></tr>");
}
>
I get an error...I "fixed" it by closing...then re-opening the
connection....b ut do I need to???
>
Why can't I open the connection and re-use the connection over and
over until I have everything..... ..as a matter of fact....I should be
able to re-use the dr also....
>
Anybody know?
>
Bob Sweeney
>
>
Feb 22 '07 #4

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

Similar topics

4
1781
by: James | last post by:
Quick question about closing recordsets and connection objects. We're in the process of rewriting a TON of bad code. None of it is even remotely tabbed properly, it's impossible to read half the time and it never closes connection objects or recordsets. Unfortunately, I've been assigned the task of closing them. I assume I'm just doing: recordset.close Set recordset = Nothing conn.close
8
2064
by: DP | last post by:
I read some articles and post how to optimize te speed of asp pages regarding opening and closing DB connections but I am still not sure about this. Is it true that it doesn't matter how many times I open and destroy a DB connection on 1 single page? ---------------------------------------------------------- Set DataConn = Server.CreateObject("ADODB.Connection") Call DataConn.Open (...)
50
11644
by: SABmore | last post by:
Is there a performance advantage to parsing thru a recordset verus using an array? I'm currently trying to populate a listbox by returning data from my database, then either parsing thru the recordset until I reach the EOF, or putting the data into an array. Thanks for your assistance.
22
6143
by: James Cane | last post by:
Here's an interesting problem that someone might have an answer to... Some time ago, I wrote a set of utility classes which wrap up the custom row source function needed to add arbitrary items to a combo or listbox. It all works nicely and allows me to do things such as sorting by clicking on column headings. Recently, all the machines here were upgraded to Access XP from 97 and I thought it might be time to take advantage of the new...
16
5729
by: Randy Harris | last post by:
I was inspired by the recent discussion of returning multiple recordsets to ADO from a stored procedure. (Amazed is probably more accurate). I asked about how to accomplish same with Oracle and got a nudge in the right direction from Mr. Kreft. I promised to provide details once working, so here it is. The code is shown below. My next step is to build this technique into my application. I'm hoping for substantial performance gain. ...
34
10855
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have VBA code that wrote the results of that away to the db, either creating new records or updating existing records, whichever was relevant. This may also include deleting records. Now I generally do this by opening a recordset on the source data...
4
2445
by: rdemyan via AccessMonster.com | last post by:
Can someone help me with creating code that will look for DAO recordsets in modules and then check to see if the recordset is also closed in the module. All of my recordsets are of the form rs* where * is a wildcard for letters after rs. Thanks. -- Message posted via AccessMonster.com
0
232
by: Bob | last post by:
I need to get several recordset for which I'm opening a datareader like so... OleDbCommand rsA = new OleDbCommand("Select * from Authors",cnAccess); OleDbDataReader drA = rsA.ExecuteReader(); while(drA.Read()) { sbL.Append("<tr><td>"+drA.ToString()+"</td></tr>"); }
8
1494
by: TimSki | last post by:
Hi, I have an application written in calssic asp which connects to a sql server 2005 db. I have 2 procs to open and close the db as follows Dim strConn Dim oConn sub OpenDataConnection()
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5536
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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 we have to send another system
3
3003
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.