[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dynamic Command Building

Posted on 2005-04-12
1
Medium Priority
?
242 Views
Last Modified: 2010-04-16
I'm using the application blocks from Microsoft to make it easier to execute my database related commands in C#.net.  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(
            thisConnection,
            CommandType.StoredProcedure,
            storedProcName,
            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?
0
Comment
Question by:Remulac
1 Comment
 
LVL 6

Accepted Solution

by:
melodiesoflife earned 1000 total points
ID: 13768864
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(
          thisConnection,
          CommandType.StoredProcedure,
          storedProcName,
          sqlParams);
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

831 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