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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:19:07+00:00 2026-05-13T10:19:07+00:00

I have a simple problem. I want to decorate the SqlDataReader class so that

  • 0

I have a simple problem.

I want to decorate the SqlDataReader class so that when the dispose or close methods are called I can dispose of a hidden resource at the same time.

the SqlDataReader class is not inheritable.

How can I accomplish this? I really don’t want to implement the DbDataReader, IDataReader, IDisposable & IDataRecord interfaces

  • 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-13T10:19:07+00:00Added an answer on May 13, 2026 at 10:19 am

    Even if you could inherit from SqlDataReader it wouldn’t matter anyway because you couldn’t make SqlCommand create an instance of your derived class.

    Implementing IDataReader in a wrapper is really not hard at all when you’re just deferring to the underlying SqlDataReader. It’s just a little time consuming but not that bad.

    But I’m curious, is the resource you want disposed the connection? If so there is a CloseConnection member of the CommandBehavior enum that ensures the connection will be closed when the data reader is closed.

    var reader = command.ExecuteReader(CommandBehavior.CloseConnection);
    ...
    reader.Close(); // also closes connection
    

    Note that Close/Dispose are the same thing on SqlDataReader.

    Finally, here’s one last suggestion that has served me well in the past. Note that in the following loose example, you own the SqlDataReader from start to finish even though you are “yielding” back to the caller at each record.

    private static IEnumerable<IDataRecord> GetResults(this SqlCommand command) {
        using (var myTicket = new MyTicket())
        using (var reader = command.ExecuteReader()) {
            while (reader.Read()) {
                yield return reader;
            }
        }
        // the two resources in the using blocks above will be
        // disposed when the foreach loop below exits
    }
    
    ...
    
    foreach (var record in myCommand.GetResults()) {
    
        Console.WriteLine(record.GetString(0));
    
    }
    
    // when the foreach loop above completes, the compiler-generated
    // iterator is disposed, allowing the using blocks inside the
    // above method to clean up the reader/myTicket objects
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is simple: I have a long list of elements that I want
I have a simple problem: I want to construct a regex that matches a
I basically have a simple problem in my program that I just want to
I have simple LINQ problem that I can't figure out. I have a table
I have simple WinForms GUI that I want to use with TabPages. The problem
I have a simple animation the problem is I want the animation to end
I have a simple problem that I have not been able to find an
I have a simple problem that I've been stuck on for some time and
I have a simple problem that i cannot solve. I have a dictionary: aa
I have a simple problem but no matter what I try I can't see

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.