5

I am creating SqlParameter Array and passing it to StoredProcedure. The output value will be set in StoredProcedures. How to access that value by parameter name?

 SqlParameter[] parameters = { new SqlParameter {ParameterName = "@test", Value = "test", Direction = ParameterDirection.Output }}; 

This works

parameters[0].Value

How to make this to work?

parameters["@test"].Value

3 Answers 3

5

This is just an array of SqlParameters, so it is going to remain ordinal as any array would work. You could probably use LINQ here (example shortly)

parameters.First(parameter => parameter.ParameterName == "@test")
Sign up to request clarification or add additional context in comments.

2 Comments

hi thanks, +1 for linq and that works. Is there any other to do that?
@SSS not that I can think of atm. You could possibly encapsulate in an extension method, but I you wouldnt gain that much
1

You can use Enumerable.First() method

Returns the first element of a sequence.

SqlParameter[] parameters = { new SqlParameter { ParameterName = "@test", Value = "test", Direction = ParameterDirection.Output } };
Console.WriteLine(parameters.First(parameter => parameter.ParameterName == "@test"));

Output will be;

@test

2 Comments

@SSS Hmm, I don't know any other way right now but if I find, I update my answer.
Thanks, both answers works. But as he answered before you i am accepting his answer. Thanks again
0

You could try this:

enum ParamNames { ReportName, Description, MaxReportsAllowed, IncludeSignatures};

SqlParameter[] param = new SqlParameter[32];
param[ReportName] = new SqlParameter("@ReportName", ReportName1.Text);
param[Description] = new SqlParameter("@Description", Description.Text);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.