dataset

  • Thread starter Thread starter Brad Coble
  • Start date Start date
B

Brad Coble

What is the best way to handle disposing of a DataSet or DataReader within a
function that returns either? Or should i just trust that its gone when it
goes out of scope?
 
I would be careful about "returning" DataReaders in most instances, as you
can end up tieing up precious resources. With DataSets, it is not as big a
deal, as it does not consume much more than memory.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
 
Hi Brad,

In .NET world, it's the garbage collector's responsibility to release and
destroy an object. In fact, we have no way to destroy an object manully or
actively in .NET world at all. After an object leaves scope, it is ready
to be destroyed by the garbage collector.

All the DataReaders available in .NET Framework, i.e. SqlDataReader,
OleDataReader, OdbcDataReader and OracleDataReader are derived from the
DbDataReader class. The DbDataReader class has implemented the IDisposable
interface, which means when we don't need to use a DbDataReader object any
more, we should call the IDisposable.Dispose method on the DbDataReader
object to release the resources the object uses immediately.

After calling the Dispose method, we set the DbDataReader object to null or
Nothing to make the object out of scope.

For DataSet, it doesn't implement the IDisposable interface. When we don't
need a DataSet object, just set it to null or Nothing to make it out of
scope.

In your scenario, because you return a DataSet or DataReader in a function,
you couldn't call the Dispose method on the returned DataReader object or
set the DataSet or DataReader to null(Nothing) at the end of the function.
But you should call the Dispose method on the DataReader object and then
set the DataReader or DataSet object to null(Nothing) when you don't need
them outside of the function.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda,

Just an addition on this.
In your scenario, because you return a DataSet or DataReader in a
function,
you couldn't call the Dispose method on the returned DataReader object or
set the DataSet or DataReader to null(Nothing) at the end of the function.
But you should call the Dispose method on the DataReader object and then
set the DataReader or DataSet object to null(Nothing) when you don't need
them outside of the function.

Be aware that the garbage collector has a fine mechanisme to find if an
object is reference is still used.

So you can set the reference to nothing(null) or dispose but that does not
mean that the object will be destructed.

The only thing is then that you cannot reference it anymore.

AFAIK happens the destruction of the object as the Garbage collector by
using 3 kind of procedures has discovered that the object is nowhere in use
anymore.


Cor
 
Back
Top