how to add a new parameter to SqlParameter[ ] Collection?

后端 未结 5 907
深忆病人
深忆病人 2021-01-20 14:46

I want to use something similar like this:

using (DataSet ds = new DataSet())
{
    SqlParameter[] dbParams = new SqlParameter[]
    {                                


        
相关标签:
5条回答
  • 2021-01-20 14:56

    Just use a list instead:

    var parameters = new List<SqlParameter>
    {
        new SqlParameter("@PromptID", promptID)
    };
    if (scenarioID != 0)
    {
        parameters.Add(new SqlParameter("@ScenarioID", scenarioID));
    }
    

    If you really need an array at the end, you can always use:

    var array = parameters.ToArray();
    
    0 讨论(0)
  • 2021-01-20 14:57

    You should use List instead of array.

       List<SqlParameter> dbParams = new List<SqlParameter>(); 
    

    and then you can do

       dbParams.Add(new SqlParameter("@PromptID", promptID);
    
    0 讨论(0)
  • 2021-01-20 15:02
    var parameters = new List<SqlParameter>
    {
        new SqlParameter("@PromptID", promptID),       
    };
    
    0 讨论(0)
  • 2021-01-20 15:02

    Other suggestion:

    SqlParameter[] sqlPar = new SqlParameter[1];
    sqlPar[0] = new SqlParameter("@System", "Teste");
    
    0 讨论(0)
  • 2021-01-20 15:17

    If you know the maximum number of items (instanses of SQLParameter type) then following approach is ideal. Else you can go with List<SqlParameter> approach, which is briefed by peers.

     var dbParams = new SqlParameter[2];
     dbParams.SetValue(new SqlParameter("@PromptID", promptID),0);
     dbParams.SetValue(new SqlParameter("@ScenarioID", scenarioID), scenarioID != 0 ? 1 : 0);
    
    0 讨论(0)
提交回复
热议问题