在预期条件的上下文中指定的非布尔类型的表达式,在 c# 中使用 DbCommand 进行参数化

An expression of non-boolean type specified in a context where a condition is expected, paramerization using DbCommand in c#

我有这个函数来执行参数化 select 查询:

public string LookUp(string sColuna, string sTabela, string sWhere)
{
    string[] Parameters = { "@column", "@table", "@where" };
    var comando = @"SELECT @column FROM @table WHERE @where";
    var cmd = this.OraConnection.CreateCommand();
    cmd.CommandText = comando;
    cmd.Parameters.AddRange(Parameters.Select(item => { var param = cmd.CreateParameter(); param.ParameterName = item; param.Value = 1; return para; }).ToArray());
    cmd.Parameters[0].Value = sColuna;
    cmd.Parameters[1].Value = sTabela;
    cmd.Parameters[2].Value = sWhere;
    DbDataReader Reader = cmd.ExecuteReader();
    return Reader.GetString(0);
}

当它到达 DbDataReader Reader = cmd.ExecuteReader(); 时,它抛出一个异常说:附加信息:在“@where”附近预期条件的上下文中指定的非布尔类型的表达式。 where 子句中不能有参数吗?

Now I've changed it to:

var cmd = this.OraConnection.CreateCommand();
            cmd.CommandText = string.Format("SELECT {} FROM {} WHERE", sColuna, sTabela, sWhere);
            DbDataReader Reader = cmd.ExecuteReader();
            return Reader.GetString(0);

String sWhere 是评估布尔表达式所需的参数

[你的查询的开始] WHERE [Something]=[你的参数]
[查询的开头] WHERE [Something]=@where

对于@where value = sWhere

如果您想了解有关构建 WHERE 子句的更复杂的答案,请查看此答案about the same question