Reputation:
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
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
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
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