I wrote this little function to do queries on a SQL Server 2005 database:
public DataSet Query(string query, Dictionary<string, string> parameters)
{
DataSet result = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(query, sqlConnection))
{
adapter.SelectCommand.CommandType = CommandType.Text;
adapter.SelectCommand.CommandText = query;
foreach (KeyValuePair<string, string> pair in parameters)
{
adapter.SelectCommand.Parameters.Add(pair.Key, SqlDbType.Text).Value = pair.Value;
}
adapter.Fill(result);
}
return result;
}
When I call it like this:
Query("SELECT * FROM Database.Tab.le WHERE somecol LIKE '%1%'", new Dictionary<string, string>() { });
I get a lot of results. But what I want is to call it like this:
Query("SELECT * FROM Database.Tab.le WHERE somecol LIKE '@number'", new Dictionary<string, string>() { {"@number", "%1%"} });
This, however, returns zero rows. I would like to know why and how to fix it.