Hello, people.
I need help with parameterizing this query.
SELECT *
FROM greatTable
WHERE field1 = @field1
AND field2 = @field2
The user should be able to search for any of the 2 fields, and the user also should be able to search if the field2 has null values.
var query = "theQuery";
var cm = new SqlCommand(cn, query);
cm.AddParameter("@field1", "352515");
cm.AddParameter("@field2", DBNull.Value);
// my DataTable here is having 0 records
var dt = GetTable(cm);
[Edit]
What is the best alternative?
Keep CommandText constant so the plan in Sql be reused
WHERE (field2 = @field2 OR @field2 IS NULL)
Change CommandText dinamically based on the values introduced by the user.
WHERE field2 IS NULL
I'm not just thinking in one field, it could be various.