user1618825
user1618825

Reputation:

How to access SqlParameter array with name

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

Upvotes: 5

Views: 4157

Answers (3)

Jim Bradshaw
Jim Bradshaw

Reputation: 1

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

Upvotes: 0

Justin Pihony
Justin Pihony

Reputation: 67065

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

Upvotes: 5

Soner Gönül
Soner Gönül

Reputation: 98740

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

Upvotes: 1

Related Questions