为什么Toad喜欢我喜欢的查询但C#不喜欢?

时间:2012-06-22 18:04:33

标签: c# sql oracle sql-like

如果我在Toad中运行此查询:

SELECT BANDID, BANDNAME
FROM WOODSTOCK
WHERE BANDNAME LIKE '%THE%'

..它只是花花公子,并返回一堆行。但是,大概相当于代码:

const string sql = @"SELECT BANDID, BANDNAME
            FROM WOODSTOCK
            WHERE BANDNAME LIKE '%:BANDNAMEPORTION%'";
. . .
ocmd.Parameters.Add("BANDNAMEPORTION", BandNamePortion);
. . .

...不返回任何记录。

2 个答案:

答案 0 :(得分:9)

嗯,“大概相当于”明显不等同,因为它不起作用。您的参数名称在引号内,因此不会将其视为参数。我怀疑你想要:

const string sql = @"SELECT BANDID, BANDNAME
            FROM WOODSTOCK
            WHERE BANDNAME LIKE :BANDNAMEPORTION";
. . .
ocmd.Parameters.Add("BANDNAMEPORTION", "%" + BandNamePortion + "%");

答案 1 :(得分:5)

另一种选择是

const string sql = @"SELECT BANDID, BANDNAME
        FROM WOODSTOCK
        WHERE BANDNAME LIKE '%' + @BANDNAMEPORTION + '%';";
. . .
ocmd.Parameters.Add("BANDNAMEPORTION", BandNamePortion);