Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is faster: SqlCommand.Parameters[string] or .Parameters[int]?

Which way is preferred?

SqlCommand = new SqlCommand(query);

command.Parameters.Add("@Foo");
command.Parameters[0].Value = Foo;
command.Parameters.Add("@Bar");
command.Parameters[1].Value = Bar;

// or

command.Parameters.Add("@Foo");
command.Parameters.Add("@Bar");
command.Parameters["@Foo"].Value = Foo;
command.Parameters["@Bar"].Value = Bar;
like image 211
abatishchev Avatar asked Nov 28 '22 19:11

abatishchev


1 Answers

Two other options:

command.Parameters.AddWithValue("@Foo", Foo);

command.Parameters.Add("@Foo").Value = Foo;

Additionally, I don't think the speed difference between any of them would be enough that you should choose based on it; Pick the one that is the most readable to you and your team.

like image 87
Chris Shaffer Avatar answered Dec 06 '22 16:12

Chris Shaffer