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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:18:33+00:00 2026-06-15T09:18:33+00:00

Could I get some help explaining this answer below and how it works with

  • 0

Could I get some help explaining this answer below and how it works with the delegate. Its the answer from here: C# abstraction and database layer

…if you are stuck on the idea of using a DataReader, you could pass a delegate to the helper, which gets invoked inside of the using statements:

public string GetMySpecId(string dataId)
{
    return _dbHelper.ExecuteQuery(
        dr => 
           {
               if(dr.Read())
               {
                   return dr[0].ToString();
               }
               // do whatever makes sense here.
           },
        @"select ""specId"" from ""MyTable"" where ""dataId"" = :dataId",
        new SqlParameter("dataId", dataId));
    return result.Rows[0][0].ToString();
}

You could also use a lightweight tool like Dapper to simplify some of the syntax and take care of mapping to your data types. (You’d still need to deal with opening a connection and such.)

  • 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-15T09:18:34+00:00Added an answer on June 15, 2026 at 9:18 am

    Declaring the ExecuteQuery Method from above should look something like this:

    public DataTable ExecuteQuery(Func<DataReader, DataTable> delegateMethod, string sqlQuery, SqlParameter param)
        {
            using (SqlConnection conn = new SqlConnection(this.MyConnectionString))
            {
                conn.Open();
    
                // Declare the parameter in the query string
                using (SqlCommand command = new SqlCommand(sqlQuery, conn))
                {
                    // Now add the parameter to the parameter collection of the command specifying its type.
                    command.Parameters.Add(param);
    
                    command.Prepare();
    
                    // Now, add a value to it and later execute the command as usual.
                    command.Parameters[0].Value = dataId;
    
    
                    using (SqlDataReader dr = command.ExecuteReader())
                    {
                       return delegateMethod(dr);
                    }
                }
            }
        }
    

    That should be right, you may have to swap the DataReader and the DataTable in the Func, I can’t remember which comes first the param types or the return type.

    Here’s another example of using the Func delegate, there’s also the Action Delegate if you don’t need a return type.

    Func Delegate Reading
    Normal Delegate Reading

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

Sidebar

Related Questions

I was hoping I could get some help from you guys. The code below
I'm new here and I hope I could get some help with an Android
I've searched for an answer and found some c#-examples, but could not get this
Could I get some help creating this query please? Users book product video slots
hoping to get some help here. I have locations on a database that I
net and wondering if I could get some get help with this problem. The
I was hoping I could get some help optimizing the following data retrieval. Here
I was wondering if i could get some help. What i am trying to
Wondering if I could get some advice and direction on this following requirement: Need
I was wondering if I could get some help with the code I pasted

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.