I am currently trying to build up the where clause of an SqlCommand
.
something similar to this
myCommand.CommandText = \"SELECT * \" +
You can't use a clause (where) with parameters, you are only allowed to use parameters with command.Parameters
.
To build a dynamic Where clause, you have to build your query based on conditions and string concatenation and then add the parameters accordingly.
Something like:
sb.Append("SELECT * FROM TABLE1 ");
if (someCondition)
{
sb.Append("WHERE XColumn = @XColumn");
myCommand.Parameters.AddWithValue("@XColumn", "SomeValue");
}
else
{
sb.Append("WHERE YColumn = @YColumn");
myCommand.Parameters.AddWithValue("@YColumn", "SomeOtherValue");
}
myCommand.CommandText = sb.ToString();