OleDbConnection, OleDbCommand and OleDbDataReader (one at a time?)...

S

SpotNet

Hi NewsGroup,

Hope this is the right news group for this. Programming data access code in
C#, .NET Framework 1.1, using OleDb classes. As an example I'm doing
something similar as such; Keeping it brief sorry about syntax...

OleDbConnection oledbcon = new OleDbConnection(dataSource);
OleDbCommand oledbcmd = oledbcon.Command(cmdText, commandType);
//commandType=StoredProcedure
oledbcmd.Parameters.Add("id", 5)
OleDbReader oleread = oledbcmd.ExecuteReader();

if (oleread.HasRows)
{
OleDbCommand oledbcmd_2 = oledbcon.Command(cmdText, commandType);
//commandType=StoredProcedure

while(oleread.Read())
{
oledbcmd_2.Parameters.Add("filterID", oleread.GetInt32(0));
oledbcmd_2.ExecuteNonQuery(); <----*******BANG (See
below)*********
oledbcmd_2.Parameters.Clear();
}

}

The rest is thoughtfully disposed of...

I'm told by InvalidOperationException class that there's already an
OleDbDataReader opened for the connection, close it before attempting
another. Two questions, 1) Does .NET consider, int
OleDbCommand.ExecuteNonQuery() a reader in a way? Despite returning an int.
Can I only have one OleDbDataReader per OleDbConnection occuring at a time?

Many Thanks and kind regards,
SpotNet.
 
S

SpotNet

Thanks G, I can now sop busting my head over trying to figure out what I was
doing wrong. No documentation I have on C# data access mentions this. Only
the examples they provide have one, which is not that obvious, coming from a
VB 6\ADO back round where you could have as many recordset objects to a
connection as you could fit. Thanks for the know G.

Regards,
SpotNet

: U¿ytkownik "SpotNet" <[email protected]> napisa³ w wiadomo¶ci
: : (...)
: > Can I only have one OleDbDataReader per OleDbConnection occuring at a
: > time?
: >
:
: Exactly, only one.
: G.
:
 
G

Grzegorz Danowski

The feature are mentioned in many articles in MSDN, for example find
"Retrieving Data Using the DataReader". There is one fragment:

"Note that while a DataReader is open, the Connection is in use exclusively
by that DataReader. You will not be able to execute any commands for the
Connection, including creating another DataReader, until the original
DataReader is closed."

Therefore you must open open second connection to use in your second
command.

Regards,
Grzegorz
 
Top