473,399 Members | 3,919 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,399 software developers and data experts.

Is it necessary to close a local OleDbDataReader before exit function?

Hi, everybody,

The codes below run under VS2003 for a long time. I want to upgrade it to VS2005. VS2005 gives me some varning messages such as "Varibles shouldn't be used before being assigned".

My question is: Is it necessary to close sDa in my code?

'-------------------------------------------------------------------------------
Private Function MethodIDExist(ByVal iMethodID As Integer) As Boolean
Dim oCmd As New OleDbCommand

If ConnMethod.State = ConnectionState.Closed Then ConnMethod.Open()
With oCmd
.Connection = ConnMethod
.CommandType = CommandType.StoredProcedure
.CommandText = "StoredProcedureName"

.Parameters.Add("", OleDbType.Integer)
.Parameters(0).Value = iMethodID
End With

Dim sDa As OleDbDataReader

Try

sDa = oCmd.ExecuteReader()
If sDa.Read() Then
Dim Num As Integer = sDa(0)
If Num 0 Then
sDa.Close()
Return True
End If
End If
If Not sDa.IsClosed Then sDa.Close()
Return False
Catch exc As Exception
MessageBox.Show(exc.ToString)
'************************************************* ********************
If (Not sDa Is Nothing) AndAlso (Not sDa.IsClosed) Then sDa.Close()
'************************************************* ********************
Return False
End Try
End Function
'--------------------------------------------------------------------------------

Thanks in advance,

Peter
Dec 26 '06 #1
2 1227
Regardless of whether or not you are exiting from a method, you should
ALWAYS close a DataReader as soon as possible after you are finished using
it.

In my opinion the method is making hard work of something that is very
simple.

If, as the logic shows, you are only interested in the value from the first
column of the first row returned by the strored procedure, then you have no
need to use a DataReader at all. The ExecuteScalar() method of the
OleDbCommand object will do the job, in conjunction with a type conversion
and a test resulting in a boolean result.

Return (CType(oCmd.ExecuteScalar(), Integer) 0)

Doing away with the DataReader object will stop the warning message from
being generated

Note too that there is an overload of the constructor for the OleDbCommand
object that takes commandtext and connection parameters. This means that you
do not have to set all the properties of the OleDbCommand object seperately.
You do, however, need to set the CommandType property seperately.

In addition, you can add the parameter object and set it's value in a single
statemment.

I note that you are 'opening' the connection object if it is not already
open and leaving it open. If that is your intention then well and good. If
however, you intend to leave it in the state you found it, then you need to
keep track of it's state and close it if you opened it in this method.

The 'improved' method would be something like:

Private Function MethodIDExist(ByVal iMethodID As Integer) As Boolean

' Save the current state of the connection
Dim _constate as ConnectionState = ConnMethod.State

If _constate = ConnectionState.Closed Then ConnMethod.Open()

Dim oCmd As New OleDbCommand("StoredProcedureName", ConnMethod)

oCmd.CommandType = CommandType.StoredProcedure

oCmd.Parameters.Add("", OleDbType.Integer).Value = iMethodID

Try
Return (CType(oCmd.ExecuteScalar(), Integer) 0)
Catch exc As Exception
MessageBox.Show(exc.ToString)
Return False
Finally
' If the connection was closed to start with then close it here
If _constate = ConnectionState.Closed Then ConnMethod.Close()
End Try

End Function
"Peter" <zl*****@sina.comwrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
Hi, everybody,

The codes below run under VS2003 for a long time. I want to upgrade it to
VS2005. VS2005 gives me some varning messages such as "Varibles shouldn't be
used before being assigned".

My question is: Is it necessary to close sDa in my code?

'-------------------------------------------------------------------------------
Private Function MethodIDExist(ByVal iMethodID As Integer) As Boolean
Dim oCmd As New OleDbCommand

If ConnMethod.State = ConnectionState.Closed Then ConnMethod.Open()
With oCmd
.Connection = ConnMethod
.CommandType = CommandType.StoredProcedure
.CommandText = "StoredProcedureName"

.Parameters.Add("", OleDbType.Integer)
.Parameters(0).Value = iMethodID
End With

Dim sDa As OleDbDataReader

Try

sDa = oCmd.ExecuteReader()
If sDa.Read() Then
Dim Num As Integer = sDa(0)
If Num 0 Then
sDa.Close()
Return True
End If
End If
If Not sDa.IsClosed Then sDa.Close()
Return False
Catch exc As Exception
MessageBox.Show(exc.ToString)
'************************************************* ********************
If (Not sDa Is Nothing) AndAlso (Not sDa.IsClosed) Then
sDa.Close()
'************************************************* ********************
Return False
End Try
End Function
'--------------------------------------------------------------------------------

Thanks in advance,

Peter
Dec 26 '06 #2
Thank you very much, Stephany. I really need to do much to improve my old
codes.

Peter

Dec 26 '06 #3

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

Similar topics

4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
1
by: Gary Cobden | last post by:
Hi I have a routine that uses VBA to open a hidden occurence of Excel, and do background computations. However, in the event that the routine terminates abnormally, I have not been able to...
4
by: James Birkholz via AccessMonster.com | last post by:
A few months ago, a working Access97 system started exhibiting this behavior. About half the time when the .mde is closed, the app window remains open and can then only be closed by using "End task"...
1
by: Ebrahim | last post by:
This message is in reply to a prev 1 . My application refues to close . Some one had suggested that I might have threads running.. but i solved that problem too . The app still refuses to close...
4
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
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...
2
by: jpr | last post by:
I am using moduled to open forms. I run them from a menu that I have created on the toolbar. The source of the forms are queries all with record source to "lstpreinterview" on a form named eforms....
3
by: Bob | last post by:
I need to get several recordset for which I'm opening a datareader like so... OleDbCommand rsA = new OleDbCommand("Select * from Authors",cnAccess); OleDbDataReader drA = rsA.ExecuteReader();...
32
by: Andy | last post by:
To further follow up on my last post regarding the docmd.quit vs. Application.quit using access 2007, I noticed that docmd.quit will correctly compact the database (program file) if you have the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.