HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Passing parameters to a query

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
passingqueryparameters

Problem

I have a function that takes a Dictionary 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 parameters = new Dictionary()
  {
    {"findBy", findByTextBox.Text}
  };
  dataGridView.DataSource = dataSource.GetDataSource(fillBy, server, parameters).Tables[0];
}

public class TableAdapters
{
  public DataSet GetDataSource(string fillBy, string server, Dictionary parameters)
  {
    string query = GetQuery[fillBy];
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = GetSqlConnection[server];
    SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
    foreach (KeyValuePair 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 GetQuery = new Dictionary()
{

Solution

You can take advantage of C#'s variable parameter arguments (keyword params). Like this -

public DataSet GetDataSource(string fillBy, string server, params KeyValuePair[] parameters)
    {
        string query = GetQuery[fillBy];
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = GetSqlConnection[server];
        SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
        foreach (KeyValuePair 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("findBy",findByTextBox.Text) 
        ).Tables[0];


and in another time you can call same function with -

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


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

Code Snippets

public DataSet GetDataSource(string fillBy, string server, params KeyValuePair<String, Object>[] parameters)
    {
        string query = GetQuery[fillBy];
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = GetSqlConnection[server];
        SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
        foreach (KeyValuePair<String, Object> 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;
    }
dataGridView.DataSource = dataSource.GetDataSource(fillBy, server, 
        new KeyValuePair<string, object>("findBy",findByTextBox.Text) 
        ).Tables[0];
dataGridView.DataSource = dataSource.GetDataSource(fillBy, server, 
            new KeyValuePair<string, object>("findBy",findByTextBox.Text), 
            new KeyValuePair<string, object>("findBy2",findByTextBox2.Text)
        ).Tables[0];

Context

StackExchange Code Review Q#56025, answer score: 4

Revisions (0)

No revisions yet.