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);
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.
Add a new SqlParameter for EACH of your parameters: SqlParameter param = new SqlParameter(); SqlParameter param1 = new SqlParameter(); param. ParameterName = "@username"; param1.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With