I'd like to create a filter for a set of SQL queries so the user may filter the SQL SELECT by up to three values. The user interface has three textboxes, each will tie to a column name in a SQL table. The user may supply one, two or three criteria via these textboxes.
Here's what I have so far. I know the if(textbox...
statements won't work, but I cannot find a way to do this. (Using "SELECT TOP 10 primaryFile FROM dbo.basket WHERE (basket.itemGuid = @itemguid) AND (basket.batchid = @batchid) AND (basket.account = @account"
won't return ANY results.
private List<string> GetSnippets()
{
List<string> snippets = new List<string>();
string connectionString = @"SNIP";
//string sql = @"SELECT TOP 10 primaryFile FROM dbo.basket WHERE";
string sql = @"SELECT TOP 10 primaryFile FROM dbo.basket WHERE (basket.itemGuid = @itemguid) AND (basket.batchid = @batchid) AND (basket.account = @account)";
//if (textBoxGUID.Text.Length > 0) sql += " basket.itemGuid = @itemguid";
//if (textBoxBatchID.Text.Length > 0) sql += " basket.batchid = @batchid";
//if (textBoxAccount.Text.Length > 0) sql += " basket.account = @account";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@itemguid", textBoxGUID.Text);
command.Parameters.AddWithValue("@batchid", textBoxBatchID.Text);
command.Parameters.AddWithValue("@account", textBoxAccount.Text);
try
{
connection.Open();
if (connection.State == ConnectionState.Open)
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
snippets.Add((string)reader["primaryFile"]);
Console.WriteLine(reader["primaryFile"]);
}
}
}
}
catch (Exception)
{
throw;
}
}
return snippets;
}