Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类似sql的问题_C#_Sql Server - Fatal编程技术网

C# 类似sql的问题

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

我正在将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 (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中的原因?没有必要