I have a SQL command text like this (there could be more parameters)
dbo.storedProcedure @JobID = 1, @JobName = 'xyz'
I want to get the parameter values and names in some kind of collection which I can then loop through and add them as SqlParameter
s.
For example:
foreach (Param in parameters)
{
cmd.Parameters.AddWithValue(param.Name, Param.Val)
}
I will then remove the parameters from the command text i.e. dbo.storedProcedure
.
The reason I want to do this is because I want the 'ReturnValue' from the procedure. I know I could just use CommandType.Text
and pass the command text as is but I always get 0 as the return value when I do it that way.
If I set CommandType = CommandType.StoredProcedure
and also use a parameter with ReturnValue
direction I get the correct return value from the procedure.
I found this which is very close but only gives me the parameter names. I also want the values too.
C# Parse SQL statement to use parameters
Thanks