Zirbo Filip
Zirbo Filip

Reputation: 300

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

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.

Upvotes: 0

Views: 1344

Answers (2)

Nițu Alexandru
Nițu Alexandru

Reputation: 714

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.

Upvotes: 1

Zirbo Filip
Zirbo Filip

Reputation: 300

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).

Upvotes: 0

Related Questions