6

I want to use something similar like this:

using (DataSet ds = new DataSet())
{
    SqlParameter[] dbParams = new SqlParameter[]
    {                        
        new SqlParameter("@PromptID", promptID)
    };

    if (scenarioID != 0)
        dbParams.Concat(new SqlParameter("@ScenarioID", scenarioID));
    //OR
    if (scenarioID != 0)
        dbParams.Add(new SqlParameter("@ScenarioID", scenarioID));
    }
}

I have searched for adding IEnumarable collection but it doesn't solve my problem. Should I create two collections and concat them? I think there must be an easy way to add an item. Any suggestions?

3
  • It's not clear why you've got that DataSet at all, by the way... Commented May 23, 2013 at 12:57
  • use List<SqlParameter> dbParams and then you can do dbParams.Add(new SqlParemeter("@PromptID", promptID) Commented May 23, 2013 at 12:58
  • 2
    @EhsanUllah - It is generally recommended that you put answers to questions in an answer. Comments are not permanent and may be deleted, so they are generally reserved for questions about questions or tangential information. Commented May 23, 2013 at 13:00

5 Answers 5

19

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();
Sign up to request clarification or add additional context in comments.

Comments

2
var parameters = new List<SqlParameter>
{
    new SqlParameter("@PromptID", promptID),       
};

Comments

2

You should use List instead of array.

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

and then you can do

   dbParams.Add(new SqlParameter("@PromptID", promptID);

Comments

0

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);

Comments

-1

Other suggestion:

SqlParameter[] sqlPar = new SqlParameter[1];
sqlPar[0] = new SqlParameter("@System", "Teste");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.