Passing parameters to a query I have a function that takes a Dictionary&lt;String, Object&gt; as an argument.  It uses this Dictionary to create parameters for a query string.

private static TableAdapters dataSource = new TableAdapters();
private void findScriptsQueryButton_Click(object sender, EventArgs e)
{
  string fillBy = findByComboBox.SelectedItem.ToString();
  string server = serverComboBox.SelectedItem.ToString();
  Dictionary&lt;String, Object&gt; parameters = new Dictionary&lt;String, Object&gt;()
  {
    {"findBy", findByTextBox.Text}
  };
  dataGridView.DataSource = dataSource.GetDataSource(fillBy, server, parameters).Tables[0];
}

public class TableAdapters
{
  public DataSet GetDataSource(string fillBy, string server, Dictionary&lt;String, Object&gt; parameters)
  {
    string query = GetQuery[fillBy];
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = GetSqlConnection[server];
    SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
    foreach (KeyValuePair&lt;String, Object&gt; p in parameters)
    {
      adapter.SelectCommand.Parameters.AddWithValue("@" + p.Key, (p.Value.ToString().Length == 0) ? DBNull.Value : p.Value);
    }
    DataSet dataSet = new DataSet();
    dataSet.Tables.Add(new DataTable());
    adapter.Fill(dataSet.Tables[0]);
    return dataSet;
  }
}


My primary problem with my code is:


I have to create a new dictionary of parameters every time before I call the function.
The parameter names are hard coded into both the dictionaries and the query strings.


The number of parameters for a query string in this particular program can be 1-11. Is there a better way for me to pass parameters to this function where I don't have to hard code the names?  Mind you I don't want to go off parameter number either, because then I've become dependent on ensuring I pass the parameters to the function in the right order.  

For brevity here is an example of one of the queries from the query dictionary that this particular instance of the function calls:

private static readonly Dictionary&lt;String, String&gt; GetQuery = new Dictionary&lt;String, String&gt;()
{
  {"Target VDN", 
   "SELECT * FROM table_name WHERE column_name = ISNULL(@findBy, column_name) "
  }, 
  // ...
}


In this case I am only dealing with one parameter but as I mentioned some of these queries in the dictionary can take up to 11 parameters.
You can take advantage of C#'s variable parameter arguments (keyword params). Like this -

public DataSet GetDataSource(string fillBy, string server, params KeyValuePair&lt;String, Object&gt;[] parameters)
    {
        string query = GetQuery[fillBy];
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = GetSqlConnection[server];
        SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
        foreach (KeyValuePair&lt;String, Object&gt; p in parameters)
        {
            adapter.SelectCommand.Parameters.AddWithValue("@" + p.Key, (p.Value.ToString().Length == 0) ? DBNull.Value : p.Value);
        }
        DataSet dataSet = new DataSet();
        dataSet.Tables.Add(new DataTable());
        adapter.Fill(dataSet.Tables[0]);
        return dataSet;
    }


params is the keyword to use for taking variable numbers of arguments. Then you can just call it with variable arguments like -

   dataGridView.DataSource = dataSource.GetDataSource(fillBy, server, 
        new KeyValuePair&lt;string, object&gt;("findBy",findByTextBox.Text) 
        ).Tables[0];


and in another time you can call same function with - 

   dataGridView.DataSource = dataSource.GetDataSource(fillBy, server, 
            new KeyValuePair&lt;string, object&gt;("findBy",findByTextBox.Text), 
            new KeyValuePair&lt;string, object&gt;("findBy2",findByTextBox2.Text)
        ).Tables[0];


This will atleast save you from defining a dictionary all the time.