Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Parameter Collection

I have 5 parameters and I want to send them to the method:

public static SqlCommand getCommand(string procedure, SqlParameter[] parameter)
{
   Sqlcommand cmd;
   return cmd
}

Can I send these paramters at one time like this?

SqlParameterCollection prm;
prm.Add(p1);
prm.Add(p2);
prm.Add(p3);
prm.Add(p4);
prm.Add(p5);
sqlcommand cmd = getCommand(prm);
like image 638
Mehmet Avatar asked Jan 08 '09 20:01

Mehmet


People also ask

What is SqlParameter?

SqlParameter(String, SqlDbType) Initializes a new instance of the SqlParameter class that uses the parameter name and the data type. SqlParameter(String, SqlDbType, Int32) Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, and the size.

How to add multiple parameters in SqlParameter in c#?

Add a new SqlParameter for EACH of your parameters: SqlParameter param = new SqlParameter(); SqlParameter param1 = new SqlParameter(); param. ParameterName = "@username"; param1.

What is the use of CMD parameters AddWithValue?

AddWithValue replaces the SqlParameterCollection. Add method that takes a String and an Object. The overload of Add that takes a string and an object was deprecated because of possible ambiguity with the SqlParameterCollection.

Which object is used to pass variable to Sqlcommand?

Command objects use parameters to pass values to SQL statements or stored procedures, providing type checking and validation. Unlike command text, parameter input is treated as a literal value, not as executable code.


1 Answers

Or create an array of parameters by hand:

SqlParameter[] parameter = {
new SqlParameter(...), 
new SqlParameter(...), 
new SqlParameter(...)
};

But I don't see what should be wrong with your approach. It simple, readable and understendable.

like image 102
Drejc Avatar answered Oct 19 '22 15:10

Drejc