Sql Parameter Collection

65,757

Solution 1

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.

Solution 2

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?

Solution 3

Using this as inspiration, this code worked for me:

List<SqlCeParameter> parameters = new List<SqlCeParameter>();

parameters.Add(new SqlCeParameter("@Username", NewUsername));
parameters.Add(new SqlCeParameter("@Password", Password));

cmd.Parameters.AddRange(parameters.ToArray());

Solution 4

Well, that won't compile because in your call to getCommand you're not passing in a string with the procedure, but as far as the array, that should work no problem.

Solution 5

Here is my code.You can use all parameter properties like name,value,type etc.

int SelectedListID = 6;
string selectedPrefix = "IP";
string sqlQuery = "select * from callHistory where ImportID=@IMPORTID and Prefix=@PREFIX"

SqlParameter[] sParams = new SqlParameter[2]; // Parameter count

sParams[0] = new SqlParameter();
sParams[0].SqlDbType = SqlDbType.Int;
sParams[0].ParameterName = "@IMPORTID";
sParams[0].Value = SelectedListID;

sParams[1] = new SqlParameter();
sParams[1].SqlDbType = SqlDbType.VarChar;
sParams[1].ParameterName = "@PREFIX";
sParams[1].Value = selectedPrefix;


SqlCommand cmd = new SqlCommand(sqlQuery, sConnection);

if (sParams != null)
{
    foreach (SqlParameter sParam in sParams)
    {
        cmd.Parameters.Add(sParam);
        Application.DoEvents();
    }
}
Share:
65,757
Mehmet
Author by

Mehmet

Updated on July 18, 2022

Comments

  • Mehmet
    Mehmet almost 2 years

    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);
    
  • Kiquenet
    Kiquenet about 9 years
    What's about nullable values for set DBNull value ?
  • Kiquenet
    Kiquenet about 9 years
    If I have SqlHelper method like this int ExecuteNonQuery(string sql, params SqlParameter[] listParams) ?
  • Kiquenet
    Kiquenet about 9 years
    What's about nullable values for set DBNull value ?