index > .NET Framework Data Access and Storage > Returning DataReaders

Returning DataReaders

What is the best practise for the following:

I have a client that uses a data access layer to get back a DataReader for binding.

The method sig for this routine is public static OracleDataReader GetReader().

But, I want to embed opening the connection within a try...catch...finally block.

So the method looks like this (pseudo-code-wise):

private OracleDataReader;

try {open conn, populate reader via command object}
catch {catch any errors}

Then return the reader. The problem is, I have to declare the Reader outside the try...catch block but this means I'm trying to return an unassigned variable. Any thoughts on this?
huysmans

Hi,

i guess you equate it to null upon instantiating to avoid the warning.

OracleDataReader reader = null;

cheers,

Paul June A. Domag




Visit my Online Blog - http://webmag.mvps.org
Paul Domag

Slightly on a tangent....

I would advise not to return DataReaders to clients.....DataReaders have an open connection to a database and on the whole this is not something you would want happening in a solution.

Will affect scalability of your app and could lead to all sorts of dramas later on...!

Dylan Morley
Dylan Morley wrote:

Slightly on a tangent....

I would advise not to return DataReaders to clients.....DataReaders have an open connection to a database and on the whole this is not something you would want happening in a solution.

Will affect scalability of your app and could lead to all sorts of dramas later on...!



Good point, what would you return instead? Populate a dataset/business object, close the connection and return that?
huysmans

Yeah thats it......anything other than DataReader!

Personally, I like the business object approach....find it makes the GUI code very clean & explicit....gives you a lot of control over the object.

DataSets are fine, but I tend to use them more more ad-hoc stuff like reports etc.

Dylan Morley
reply 5

You can use google to search for other answers

 

More Articles

• parameter Array in Stored Procedure
• How to Manage Transactions
• SQL Database Shema Update
• TableAdapter insert/update/delete order
• ADO.Net Performance
• Concurrency violation while row exists
• Access problem - query returns some empty rows and some with data
• Problems with running an Access macro in VS 2005
• Finding orphans among two datatables
• calling database in aspx file
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• ODBC Wizard can't create insert/update/d
• The provider could not determine the Dec
• DataTable.Load() Generates InvalidOperat
• Dataset to Xml file with sub nodes
• Dropdownlist Binding
• mapping issue with sql executenonquery m
• Cannot find stored procedure <storedp
• Active Directory+Database
• Error when saving a "Computed"
• Repeating name in namespace + classname,
• VB.Net Data Storage
• Can a DataSet tell if tables in the SQL
• Stored Procedures takes long time to exe
• Import data from Excel
• How to get headers only from column name

Hot Articles

• Help with queries
• Size of Dataset rows in BYTES
• Returning Exceptions with ExecuteScaler
• Seek suggestion on .NET support options
• add parameters to oledb
• import / parse DIF files (Data Interchan
• ConfigurationManager.AppSettings not rea
• Update without Parameter
• Oracle Connectivity Problem
• Same Problems
• Issue in reading data from an Excel file
• multiple to single - oracle help req.
• interacting applications
• [OTP]mouseover and open new page
• UPdating a SQL table using dataset from

Recommend Articles

• Issues with updating Access DataSource
• Enterprise Library problem with oracle 1
• Create table in code
• A problem when trying to create a SQL da
• Simple Update from a VS 2005 vb project
• SQLDataReader.GetOrdinal() fails rarely
• Dropdownlist Binding
• problem copying table from one database
• VB Express creating new project
• DateTime Conversions
• FAQ
• DataView RowFilter not filtering on perc
• Query a View
• Calculated Column --> Using Custom Ag
• Any way to display images in gridview?