Only values are considered to be parameters in queries.
The attributes you reference are never parameters.
This is a basic rule of SQL and many other query language implementations.
But you can code around this constraint as shown below.
Mind that the actual attribute names should NEVER come from the outside (user or database) but be hard-coded for security reasons. Not doing so might expose you to SQL injection and similar attacks.
To summarize:
- values are parameters
- attributes are hard-coded
This is the source code you should adapt to your problem:
QueryCase querycase;
string querytext, attributeName1, attributeName2;
querycase = QueryCase.Alpha; // switch between possible queries
// somewhere later in your code...
switch (querycase)
{
case QueryCase.Alpha:
attributeName1 = "attribute1";
attributeName2 = "attribute2";
case QueryCase.Beta:
attributeName1 = "attribute3";
attributeName2 = "attribute4";
default:
throw new NotImplementedException(string.Format("unrecognized query case (was {0})", (int)querycase));
}
querytext = string.Format("select * from table1 where {0}=@1 and {1}=@2", attributeName1, attributeName2);
adp.SelectCommand.CommandText = querytext;
Here, querycase is a hard-coded enum so that you cannot set arbitrary attribute names:
enum QueryCase
{
Alpha,
Beta
}
@3and@4? Also, you should realize that there's not enough information on what your problem might be.