PC Review


Reply
Thread Tools Rate Thread

DataReader question

 
 
=?Utf-8?B?VG9kZCBCcmlnaHQ=?=
Guest
Posts: n/a
 
      16th Feb 2005
Is the following code a valid way of passing a DataReader object back to
calling code? I'm specifically concerned with the objConn object be
explicitly set to Nothing. Does this cause the connection object to be
garbage collected or does the connection object not get destroyed until the
calling code closes the DataReader object (and thus the connection object)?

Public Function GetDataReader(ByVal strSelect As String) As
OleDbDataReader
Try
objConn = New OleDbConnection(strConnectionString)
objCmd = New OleDbCommand(strSelect, objConn)
objCmd.Connection.Open()
GetDataReader =
objCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As OleDbException
GetDataReader = Nothing
Finally
objConn = Nothing
objCmd = Nothing
End Try
End Function

 
Reply With Quote
 
 
 
 
William \(Bill\) Vaughn
Guest
Posts: n/a
 
      16th Feb 2005
Since you coded "CommandBehavior.CloseConnection), the client should (must)
close the DataReader when it's done with it. This will close the Connection
(return it to the pool) and mark its resources for cleanup on next GC.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Todd Bright" <(E-Mail Removed)> wrote in message
news:C6C379CA-C095-404C-A792-(E-Mail Removed)...
> Is the following code a valid way of passing a DataReader object back to
> calling code? I'm specifically concerned with the objConn object be
> explicitly set to Nothing. Does this cause the connection object to be
> garbage collected or does the connection object not get destroyed until
> the
> calling code closes the DataReader object (and thus the connection
> object)?
>
> Public Function GetDataReader(ByVal strSelect As String) As
> OleDbDataReader
> Try
> objConn = New OleDbConnection(strConnectionString)
> objCmd = New OleDbCommand(strSelect, objConn)
> objCmd.Connection.Open()
> GetDataReader =
> objCmd.ExecuteReader(CommandBehavior.CloseConnection)
> Catch ex As OleDbException
> GetDataReader = Nothing
> Finally
> objConn = Nothing
> objCmd = Nothing
> End Try
> End Function
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
datareader question =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= Microsoft ASP .NET 6 16th Feb 2007 04:52 PM
DataReader question Walid Magd Microsoft ADO .NET 2 19th Oct 2005 01:55 PM
DataReader Question Patrick Olurotimi Ige Microsoft ASP .NET 2 21st Mar 2005 09:02 AM
DataReader Question Jerry Microsoft ADO .NET 3 22nd Oct 2003 07:16 AM
DataReader question?? kevin Microsoft ADO .NET 1 7th Oct 2003 09:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 AM.