【发布时间】:2014-01-03 13:56:45
【问题描述】:
我正在使用以下功能来检索选择记录。我给了表格我的列名和值,它显示了结果。但问题是,它没有将列名作为参数,例如:
public List<Products> ListAllProducts(string searchOption, string searchValue)
{
db.ClearParameters();
db.AddParameter(db.MakeInParam("@ColumnName", DbType.String, 50, searchOption));
db.AddParameter(db.MakeInParam("@Value", DbType.String, 50, searchValue));
string query = @"SELECT *
FROM [Products]
WHERE @ColumnName LIKE '%'+@Value+'%'";
ds = db.GetDataSet(query);
//Rest of code but above query is not executing
}
但是当我使用这样的查询时:
string query = @"SELECT *
FROM [Products]
WHERE "+searchOption+" LIKE '%'+@Value+'%'";
它运行良好并给我结果。我读过this、this 和this one specially,但不知道。 请指导我。
【问题讨论】:
标签: sql sql-server