C# Run a procedure without specifying a parameter name

The name of the parameter in the stored procedure may change from CustomerID to CustID

Slap the person who does that.

Parameter names are your reliable way of identifying a parameter. The other option is sequence, seems a lot more flaky.


I don't think you can create a SqlParameter object without specifying its name. However, you should be able to use the DeriveParameters method (see MSDN) to get a collection of parameters with the names automatically retreived from the SQL server.

You can find an example here. It looks roughly like this:

SqlCommand command = // create a command for calling the stored procedure
SqlCommandBuilder.DeriveParameters(command);

// Now you can set values of parameters in a loop
for(int i = 0; i < command.Parameters.Length; i++) {
  var parameter = command.Parameters[i]
  // Set value of ith parameter
}