Dynamic Command Building

Posted on 2005-04-12
Last Modified: 2010-04-16
I'm using the application blocks from Microsoft to make it easier to execute my database related commands in  I'm trying to write a generic function that populates a datagrid based on these application blocks.

WHen it comes time to get the data to bind to the grid, I use:

      // Call stored procedure to get back field names from specific table
      DataSet ds = SqlHelper.ExecuteDataset(
            new SqlParameter(argName,argValue));

What if I wanted to tack on a variable number of parameters at the end of the above command?  In other words, what if I needed to send two or three parameters to the stored proc instead of just one?  I've created an array of sql parameters, but how would I build it onto the above command?  Ideally, I would be able to execute a string like I can in Java.  Can this be done in C#?

I guess my other option is to not use application blocks in this case and just build everything from scratch, thereby adding the flexbility I need.  Any ideas?
Question by:Remulac
    1 Comment
    LVL 6

    Accepted Solution

    Please try:

    SqlParameterCollection sqlParams = new SqlParameterCollection();

    sqlParams.Add(new  SqlParameter(argName1,argValue1));
    sqlParams.Add(new  SqlParameter(argName2,argValue2));
    sqlParams.Add(new  SqlParameter(argName3,argValue3));
    DataSet ds = SqlHelper.ExecuteDataset(

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now