Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:59:04+00:00 2026-05-17T00:59:04+00:00

I’m trying to make sure that I don’t leave any loose ends open in

  • 0

I’m trying to make sure that I don’t leave any loose ends open in my application and am concerned about a few but might get my answer from this one. I’ve “overriden” some functions so that way I can try and keep all the resources as clean and free as possible. So in this instance, I have a function called ExecuteReader which returns a DbDataReader as normal, but all I had to pass to it was a SQL string rather than recreating a DbCommand every time. I want to make sure that even though I’m unable to call dbCommand.Dispose() that it is actually doing so. Any and all help is appreciated.

Public Function ExecuteReader(ByVal strSQL As String) As DbDataReader
    Dim dbCommand = _dbConnection.CreateCommand()
    dbCommand.CommandText = strSQL
    dbCommand.Prepare()
    Return dbCommand.ExecuteReader()
End Function

I was thinking of using a using statement, but I remember seeing a thread where someone said they think it was causing them problems having a return in the using statement. Also, I’m not sure if this should be community wiki or not. If it should be, let me know. Thanks.

Code Update:

Here’s an example of how I’m using it.

Public Sub RevertDatabase()
    'This function can be used whenever all changes need to be undone, but was created'
    'for saving the data as a .out file.  It sets all changes back to their original value.'

    'Set the data reader to all parts and columns that were changed.'
    _dbReader = ExecuteReader("SELECT PART_ID, PART_PREV_VALUE, REPORT_COLUMN_NAME FROM REPORTS WHERE PART_PREV_VALUE NOT NULL")
    'Create an instance of the Command class.'
    Dim cmd = New Command()

    While _dbReader.Read()
        'For each part and columns that has been changed, set the values in the'
        'new cmd variable and then update the value using the same function'
        'that is used whenever a value is changed in the data grid view.'
        cmd.CommandString = _dbReader("REPORT_COLUMN_NAME").ToString().Replace("_", " ")
        cmd.Value = _dbReader("PART_PREV_VALUE").ToString()
        cmd.ID = _dbReader("PART_ID").ToString()
        UpdateValue(cmd)
    End While

    'Close the reader.'
    _dbReader.Close()
End Sub

In here, I set the _dbReader to what I’d get from the function and eventually I close the _dbReader. I do not close the connection as I don’t open it each time I make a query. This is a SQLite database that only one user will be using at a time (small application with very very very little likeliness it will grow) so I didn’t think it necessary to close and open the connection all the time. Maybe I’m wrong, not sure. Using it this way though, it is potentially ok for cleaning resources?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:59:05+00:00Added an answer on May 17, 2026 at 12:59 am

    It’s a bad idea passing back DbDataReader as this requires the stream to be kept open and relies on calling code to do the right thing by disposing the reader. It’s also makes it difficult to close the underlying command and connection objects. One way to facilitate this if you really want to expose the reader, is to use CommandBehavour. This can be used to close the underlying connection when the reader itself is closed.

    dbCommand.ExecuteReader(CommandBehavour.CloseConnection)
    

    As DbConnection, DbCommand and DbDataReader are Disposable you need to refactor the code to allow for these to be cleaned up when the code has finished with them. One way is to achieve this is to implement IDisposable within your own class and bubble dispose though to any encapsulated objects. The calling code can then implement using to ensure resources are freed.

    Using helper As New DatabaseHelper()
       Using reader As IDataReader = helper.LoadSomeDataReader()
    
          ' do something with reader
    
       End Using
    End Using
    

    UPDATE:

    Your second block of code would become more like:

    Public Sub RevertDatabase()
       Using _dbReader As IDataReader = ExecuteReader(...)
    
          While _dbReader.Read()
    
             Using cmd As New Command()
                cmd.CommandString = _dbReader("REPORT_COLUMN_NAME").ToString().Replace("_", " ")
                cmd.Value = _dbReader("PART_PREV_VALUE").ToString()
                cmd.ID = _dbReader("PART_ID").ToString()
                UpdateValue(cmd)
             End Using
    
           End While
    
        End Using
     End Sub
    

    You should always open and close the connection, rather then just leaving it open. It’s could practice and although highly unlikely in a very small application, you could run into issues. You also shouldn’t be keeping a reference to the _dbReader IMO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.