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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    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(B yVal iMethodID As Integer) As Boolean
    Dim oCmd As New OleDbCommand

    If ConnMethod.Stat e = ConnectionState .Closed Then ConnMethod.Open ()
    With oCmd
    .Connection = ConnMethod
    .CommandType = CommandType.Sto redProcedure
    .CommandText = "StoredProcedur eName"

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

    Dim sDa As OleDbDataReader

    Try

    sDa = oCmd.ExecuteRea der()
    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
  • Stephany Young

    #2
    Re: Is it necessary to close a local OleDbDataReader before exit function?

    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.Exe cuteScalar(), 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(B yVal iMethodID As Integer) As Boolean

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

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

    Dim oCmd As New OleDbCommand("S toredProcedureN ame", ConnMethod)

    oCmd.CommandTyp e = CommandType.Sto redProcedure

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

    Try
    Return (CType(oCmd.Exe cuteScalar(), 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.Clos e()
    End Try

    End Function


    "Peter" <zlxmqyt@sina.c omwrote in message
    news:uNhdU9IKHH A.4376@TK2MSFTN GP03.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(B yVal iMethodID As Integer) As Boolean
    Dim oCmd As New OleDbCommand

    If ConnMethod.Stat e = ConnectionState .Closed Then ConnMethod.Open ()
    With oCmd
    .Connection = ConnMethod
    .CommandType = CommandType.Sto redProcedure
    .CommandText = "StoredProcedur eName"

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

    Dim sDa As OleDbDataReader

    Try

    sDa = oCmd.ExecuteRea der()
    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


    Comment

    • Peter

      #3
      Re: Is it necessary to close a local OleDbDataReader before exit function?

      Thank you very much, Stephany. I really need to do much to improve my old
      codes.

      Peter





      Comment

      Working...