0

Hi it's possible to use the SQLCommand to work with Stored Procedures but without the need of specify the name of each parameter?

We have always to have the names of SP variables in some collection (like array of strings) to work with the SP?

The common use is this:

sqlComm.Parameters.Add("@name_SP_variable", "some_value");

What i mean if it's possible to do something like this:

sqlComm.Parameters.Add("some_value");

If we just send the right number of parameters and in the right order, can't we just put the values without the name of the variables of the SP?

2
  • 1
    No. And it doesn't lead to code that is clearer. Commented Apr 26, 2011 at 2:55
  • Can you guarantee that your code and database will always 100% be in sync and nobody will ever accidentally change the order of either the parameters in code or in the stored proc? I'd put the parameter names in. Commented Apr 26, 2011 at 3:31

1 Answer 1

1

Take a look at SqlCommand.DeriveParameters(). This post has an example usage.

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

3 Comments

But doing is a good or a bad idea? I am just asking this because if it's the better way, so we always need to have some collection with all Names of SP Parameters...
I'm not very familiar with stored procedures. Maybe someone else will comment on it.
@Miguel Are you trying to automatically generate the parameters for your SQL command? I'm a bit confused by your statement that "we need to have a collection with the names." Normally, when you construct the SQL command in your C# code, you know the names of the parameters or can easily look them up. That is, the names of the parameters are in your head as you write your code.

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.