title: Create SQL parameters programmatically
body: <p>Another annoying one for me but probably something simple.</p>
  
  <p>I have a number of possible where clauses for a query based on user input, my question is how can I add these programmatically?</p>
  
  <p>For instance:</p>
  
  <pre><code>wherequery = @"WHERE fieldname = @p_FieldName AND ";
  if (txtValue.textLength &gt; 0){
      wherequery += "fieldname2 = @p_FieldName2 AND ";
  }
  query = @"SELECT * FROM tabe" + wherequery;
  sql = connection.CreateCommand();
  sql.CommandText = query;
  </code></pre>
  
  <p>How would I go about doing the parameters for that?  I've tried ArrayLists, Dictionaries and a few other methods but can't find a way of doing it.  Ideally I'd want to do something like this:</p>
  
  <pre><code>SqlParameter[] sqlparams;
  wherequery = @"WHERE fieldname = @p_FieldName AND ";
  if (txtValue.textLength &gt; 0){
      wherequery += "fieldname2 = @p_FieldName2 AND ";
      sqlparams.Parameters.Add("@p_FieldName2 ", SqlDbType.VarChar).Value = txtValue.text;
  }
  query = @"SELECT * FROM tabe" + wherequery;
  sql = connection.CreateCommand();
  sql.CommandText = query;
  sql.Parameters.Add(sqlparams);
  </code></pre>