473,378 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

return datareader and close connection

I have some methods that open a database connection, get some data and then
return a datareader. How do I manage closing the connection to the database
then?

public OracleDataReader ExecuteCommand(string cmdStr) {
Connect();
OracleCommand cmd = new OracleCommand(cmdStr, this._conn);
OracleDataReader reader = cmd.ExecuteReader();
return reader;
}

What's the correct way to do this, even if it means totally changing this
method?
Nov 17 '05 #1
3 19487
Hi,

You can't a DataReader is a server side , forward only cursor. The
connection will be open until you close the Reader.

Therefore you have to use it very carefully.
Tip:
You could use ExecuteReader( CommandBehaviour.CloseConnection)

In this case once you close the reader the connection will be close too.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
I have some methods that open a database connection, get some data and then
return a datareader. How do I manage closing the connection to the
database
then?

public OracleDataReader ExecuteCommand(string cmdStr) {
Connect();
OracleCommand cmd = new OracleCommand(cmdStr, this._conn);
OracleDataReader reader = cmd.ExecuteReader();
return reader;
}

What's the correct way to do this, even if it means totally changing this
method?

Nov 17 '05 #2
"Craig" <Cr***@discussions.microsoft.com> schrieb im Newsbeitrag
news:76**********************************@microsof t.com...
I have some methods that open a database connection, get some data and then
return a datareader. How do I manage closing the connection to the
database
then?

public OracleDataReader ExecuteCommand(string cmdStr) {
Connect();
OracleCommand cmd = new OracleCommand(cmdStr, this._conn);
OracleDataReader reader = cmd.ExecuteReader();
return reader;
}

What's the correct way to do this, even if it means totally changing this
method?

Since the datareader has to remain open while the datareader is open, you
have to
wait for the caller using the datareader to close it.
You should call ExecuteReader(CommandBehavior.CloseConnection)
this will close the Connection when the reader is closed.
Still the caller is responsable for closing the reader.
Nov 17 '05 #3
Craig,

You can definitely follow Christof's and Ignatio's advice. However, if
you don't trust the code that is calling this method (because you don't want
it to be able to access your database through the connection), their
recommendations won't work. Rather, you will have to encapsulate your
operations in the method, and not return the data reader.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
I have some methods that open a database connection, get some data and then
return a datareader. How do I manage closing the connection to the
database
then?

public OracleDataReader ExecuteCommand(string cmdStr) {
Connect();
OracleCommand cmd = new OracleCommand(cmdStr, this._conn);
OracleDataReader reader = cmd.ExecuteReader();
return reader;
}

What's the correct way to do this, even if it means totally changing this
method?

Nov 17 '05 #4

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

Similar topics

4
by: billd | last post by:
I have a function that returns a SqlDataReader created by: result = command.ExecuteReader(CommandBehavior.CloseConnection); I then bind the result to a grid control: myDataGrid.DataSource =...
6
by: LP | last post by:
Hi, I am starting a new thread from the main UI thread. If users clicks cancel button, the thread is aborted: workerThread.Abort(); ThreadAbort Exception is handeled in the worker thread: ...
4
by: mescano | last post by:
I am currently implementing a singleton pattern for accessing a database. Is it advisable to close the connection to the database at all -- thus leaving it open or should it be closed. If closed,...
6
by: Kalim Julia | last post by:
Is it possible to open more than 1 datareader / dataadapter on one connection ? Is it possible to duplicate / clone connection ?
5
by: Varangian | last post by:
Hello there people, I'm having some kind of problem. I have a function that returns a datareader. At some point using the application I get an error "Unspecified error" (ssssoooo helpful) :). I...
5
by: jjmraz | last post by:
Hi, I have a situation where in a dll a SqlDataReader is created on a function call but is never closed. The datareader gets passed back to the asp.net page calling it. How should I close the...
0
by: bonita | last post by:
In my ASP.NET page, I have 2 checkboxes for users to choose which crystal report they want to display. These two reports use different tables. If report1 has been choosen and displayed in the...
1
by: DotNetDev | last post by:
Hi, I have a function.... private bool myfunction() { SqlDataReader myReader = CommandObject.ExecuteReader(); if (myReader.HasRows) return true; else
4
by: Max2006 | last post by:
Hi, I saw the Business Logic Layer pattern suggested at the following link and I found that the pattern never closes the connection: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.