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 7669543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:37:01+00:00 2026-05-31T15:37:01+00:00

I have created the following method: public System.Data.OleDb.OleDbDataReader GetReader(string sqlQuery) { System.Data.OleDb.OleDbConnection myConnection =

  • 0

I have created the following method:

 public System.Data.OleDb.OleDbDataReader GetReader(string sqlQuery)
  {

          System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection();
          LoadConnectionStrings();
          myConnection.ConnectionString = ConnectionString;
          myConnection.Open();
          System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(sqlQuery, myConnection);
          System.Data.OleDb.OleDbDataReader myReader = null;
          myReader = myCommand.ExecuteReader();

          return myReader;

  }

Several thousand lines of code rely on it, and i guess i wasn’t really thinking when i implemented it…

Anyway,. If i retrieve a reader this way i have no way of closing the connection, and if i close the connection before the reader has called the read() method it will blow up when it goes to read and say that the connection to the database needs to be Open.

The question is, how can i close connection from previous bodies of code? Or all connections in general maybe..

From what I’ve read here if you don’t specifically call ‘close()’ it doesn’t get closed, and if you’re using an access 2003 file it kind of leaves you in a world of hurt

  • 1 1 Answer
  • 0 Views
  • 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-31T15:37:02+00:00Added an answer on May 31, 2026 at 3:37 pm

    In that scenario, the connection needs to stay open for the reader to work – however, there is an overload to ExecuteReader that lets you specify a flag for the reader to close the connection when the reader is closed:

    myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    

    This allows a caller to use:

    using(var reader = GetReader(query)) {
        //...
    }
    

    And the connection will be closed cleanly when the caller is done with the reader.

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

Sidebar

Related Questions

I have created the following project structure for my new asp.net mvc project any
I have created the following method so as to create unique random numbers .
I have created the following function: public void DelegatedCall(Action<Object> delegatedMethod) And defined the following
I created the following extension method for a ViewPage: using System.Web.Mvc; namespace G3Site {
I have the following method: def call_http_service(url, url_params) begin conn = create_connection(url) resp =
I have following spec to test controller method: context #create do it should redirect
I have a method that contains the following (Java) code: doSomeThings(); doSomeOtherThings(); doSomeThings() creates
I have created following rule in the .htaccess file located at the root of
I have created following thing in android using android compatibility support package Basically i
I have created the following C library for reading an image: typedef struct {

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.