C# 类似sql的问题
我正在将sql查询写入我的SqlCommand,其中的某些部分如下所示:C# 类似sql的问题,c#,sql-server,C#,Sql Server,我正在将sql查询写入我的SqlCommand,其中的某些部分如下所示: WHERE Path like N'+(select top 1 path from [Paths] where objectcode=@objectCode and objecttype=@objectType)+%' order by path desc,objecttype desc 我没有得到任何记录,但当我在SQLServer2005中编写相同的记录时,我有很多行 where path like (selec
WHERE Path like N'+(select top 1 path from [Paths] where objectcode=@objectCode and objecttype=@objectType)+%' order by path desc,objecttype desc
我没有得到任何记录,但当我在SQLServer2005中编写相同的记录时,我有很多行
where path like (select top 1 path from [Paths] where objectcode='eg' and objecttype='0')+'%'
按路径描述排序
怎么了
@对象代码为“eg”
我看到了一些东西:
我忘了添加cmd.parameters,我没有得到错误,所以我认为sql将这些参数视为普通字符串,没有从中获得值,因此:
例如:
它得到了'objectcode=@objectcode'
,
而非
'objectcode='EG'
SQL将不会替换@objectcode参数,因为它位于字符串文本中
您需要去掉字符串文字,使用更类似于:
WHERE Path like (select top 1 path from [Paths] where objectcode=@objectCode and objecttype=@objectType)+'%' order by path desc,objecttype desc
您是否有将子查询包含在speechmarks中的原因?没有必要