Sql Parameter Collection


Question

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);
1
13
3/2/2010 7:45:38 PM

Accepted Answer

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.

16
1/8/2009 8:44:46 PM

I don't see what's wrong with that? You do know that, if this is .NET, you need to attach the parameters to the SqlCommand object?

So:

SqlCommand query = new SqlCommand(sqlString, Connection);
query.Parameters.AddWithValue(parameter,valueToPass);

etc?

Sorry if that's not related, not completely sure on your question? Your method doesn't really do anything, I take you left out the code and just put in a dummy for the purposes of asking the question? You can pass an array as an arguement so you just need to spilt it up?


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon