dbCommand and stored procedure parameters name different but value is passed. Why?

1.3k views Asked by At

I use C# and I instantiate a DbCommand in order to execute an Oracle stored procedure.

My question is: why does the procedure receive the value through a different named parameter than the on in db?

When I add a parameter to the dbCommand:

...
string value = "Whatever"

db.AddInParameter(dbCommand,"WrongParamName",DbType.String);
db.SetParameterValue(dbCommand, "WrongParamName", value); 

and I execute:

dataSet = db.ExecuteDataSet(dbCommand);

It will pass the dbCommand parameter to the stored procedure parameter correctly.

  • Why is that?

  • Does it set the value to the first parameter without a value or is it based on position?

  • If it's based on position why do we need the name for?

  • Is the name only to help the dev understand the code?

2

There are 2 answers

0
Nițu Alexandru On BEST ANSWER

I need to know if you execute the stored procedure like "EXEC sp_StoredProcedure @Param1 = @Param1, @Param2 = @Param2" or "EXEC sp_StoredProcedure @Param1, @Param2". In the first case, if you change the order of parameters in the sp, the call won't be affected. In the second case on the other hand, it does, because first parameter from the command fulfill the first parameter from the sp, the second to second and so on. If you cannot obtain a list of parameter names you can at least ask the other developer to not change the order and add parameters only to the end of the parameter list in stored procedure. If you cannot do this either, you have nothing else to do then to pray not to change them. With some scripts, you can determine the list of parameters though (see this How to get stored procedure parameters details?). You can execute this command like an ordinary select statement.

0
Zirbo Filip On

After further investigation it seems that dbCommand passes parameters by order, not by name because this is how dbCommand is supposed to behave.

I did not find the purpose of the name, other than it only helps the developer to know which parameter is which.

I also did not find any property in dbCommand to set BindByName (a property in OracleCommand).