1

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.)

7
  • Which part do you need explained? Commented Dec 4, 2012 at 23:23
  • If you look at the example in the other question, how do you declare the ExecuteQuery and why are there 2 return statements here? Commented Dec 4, 2012 at 23:24
  • as far as the 2 return statements go it looks like a typo, what i think he meant to put where the first return is is var result. I'll see if I can find you an example real quick on how to use a delegate. Commented Dec 4, 2012 at 23:27
  • cool thx, i'm a php programmer transitioning to .net Commented Dec 4, 2012 at 23:30
  • no worries, just trying to locate a good example, digging through some old code Commented Dec 4, 2012 at 23:31

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.