Solved

Data Layer/DataReader

Posted on 2003-11-06
5
615 Views
Last Modified: 2012-05-04
I've created a data layer class, and have coded all my database connections, data objects, etc. within this class. I call a function from an ASP.NET page -- GetDataReader --- that returns a DataReader object. My understanding is that when using a DataReader object, the connection is not closed until the DataReader object is closed using CommandBehavior.Close. If I issue the close call within GetDataReader, my calling page fails because there no longer exists any data. How do I close the DataReader after it's been passed to the calling page without losing my data --- or is there a better way of implementing this?

Thanks
0
Comment
Question by:Junoti
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
_TAD_ earned 250 total points
ID: 9697446
First, you have some mis-conceptions.

A Database Connection is dstinctly different from a datareader.

Think of a datareader as a forward-only recordset or cursor.

When you close the datareader, you may still have an open connection, but the datareader is strongly linked to the dataconnection.  If you close the connection, you close the datareader.


As for your question... persisting data after closing your connection.

I suggest loading your data into a datatable.

You can think of a dataTable a dis-joint recordset that you can copy and pass along at will completely outside of an active data connection.

There are two easy ways to fill a dataTable from a database.

1) use a data reader
2) use a dataAdapter.

A datareader and a dataAdapter are used pretty much in the same way.  A dataReader is much faster, but a DataAdapter has a ton more functionality.
0
 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 250 total points
ID: 9697513

load a dataTable from a dataReader


public dataTable GetData()
{

//Create a Connection Object

//open connection

//Create a DataCommand Object and open the Data Reader Object


//Pseudo Code
DataTable dt = new DataTable();

for(int i=0,i<DataReader.Columns.Count;i++)
       dt.Columns.Add(dataReader[i].Name);

While(DataReader.Read())
{
    DataRow dr = Dt.newRow();

    for(int i=0,i<DataReader.Columns.Count;i++)
       dr[i] = DataReader[i].Value;

    dr.AddRow(dr);
}


return dt;

}
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9697526
load a dataTable from a dataAdapter


public dataTable GetData()
{

//Create a Connection Object

//open connection

//Create a DataCommand Object and open the DataAdapter Object


DataTable dt = new DataTable();

DataAdapter.Fill(dt);

return dt;
}
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9697555


Oh... and one last thing...


*just before* the "return dt" command you should close or dispose everything.


DataAdapter.Close();
DataReader.Close();
DataCommand.Dispose();
etc...



As for programming tactics...

As you can see the DataAdapter is much easier, but there is some extra overhead.

I leave it to you to decide which method to use, they both work equally well.

From my experience I used the DataAdapter for loading data that I wish to present and I use the DataReader when I do internal scans for the program or returning a single result.


That is, If I want to check for the presence of a particular value or record in a table in order for the program to do something, I use a datareader.  If I am going to display the data and have the user interact with it I use the dataAdapter.
0
 
LVL 1

Author Comment

by:Junoti
ID: 9697561
Thanks _TAD_ for the extra comments! I'm always looking for "best practices" type information!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now