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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:57:52+00:00 2026-06-02T17:57:52+00:00

In java, you cannot access the ResultSet once the Statement is closed, although many

  • 0

In java, you cannot access the ResultSet once the Statement is closed, although many times you get a small amount of fetched data making it appear to be open. But in general it is against the API rules.

But in OLEDB, I’m wondering if the same rule applies with the Command and the reader. For instance, is the following code 100% safe? Will the SQL fetch cursor remain open even though command is disposed?

public OleDbDataReader ExecuteReader(string sql, OleDbConnection connection)
{
  using (OleDbCommand command = CreateCommand(sql, connection))
  {
     return command.ExecuteReader();
  }
}
  • 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-06-02T17:57:53+00:00Added an answer on June 2, 2026 at 5:57 pm

    This is not recommended at all. Why would you do it?

    When the Command is Disposed (this happens when the execution leaves the using block), the connection won’t be closed, so you can probably keep using the DataReader.

    However this is a very bad idea:

    • you can’t get sure that the connection will be closed, because it depends on external code.

    • this can put locks in the DB so that you’ll have contention problems.

    • the connection used by your Reader keeps “busy” so that it cannot be reused.

    You should always make sure that the reader, and then the related connection, are closed. With this design you should rely on the user of the returned DataReader to Close the reader and the connection. Or at least the reader, if you use the SqlCommand.ExecuteReader overload that receives a CommandBehavior in which you can set CloseConnection, so that the conenction will be closed automatically when the reader is closed.

    But I insist, you should never keep a DataReader open for too long. The datareader should be iterated and closed as soon as possible. It’s available to avoid the overhead of transferring data to DataTables, and the using it. I.e. when you bind to a control, like a DataGridView, you can bind the data directly to the control avoiding the intermediate use of a DataTable, which requires extra processing time and memory (data to datatable and then to control, instead of data directly from datasource to control).

    So, perhaps you can do that, but deefinitely, you shouldn’t!

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

Sidebar

Related Questions

Pack.java imports pack.TestPack; but it cannot access it. I cannot understand why it cannot
In my clojure program I cannot access package scoped fields of the java class
Obviously, JavaScript itself cannot access the filesystem. Thank goodness. But Flash, Java and other
$ javac GetAllDirs.java GetAllDirs.java:16: cannot find symbol symbol : variable checkFile location: class GetAllDirs
$ javac TestExceptions.java TestExceptions.java:11: cannot find symbol symbol : class test location: class TestExceptions
I cannot understand the Java memory usage. I have an application which is executed
I cannot download file using java if the url contains special characters. eg:-http://something.com/something/this+this+this.html http://something.com/something/this%20this%20this.html
In Java, constructors cannot be recursive. Compile time error: recursive constructor invocation. Let's assume
$ javac TestFilter.java TestFilter.java:19: non-static variable this cannot be referenced from a static context
I'm getting a class cast exception namely java.lang.ClassCastException: android.app.Application cannot be cast to greendroid.app.GDApplication

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.