传递参数设置为SQL SELECT语句子句中的行为怪异。
问题描述:
我已经得到了下面的查询返回2条记录(在DataSet中的查询生成器)
I've got the following query that returns 2 records (in DataSet's query builder)
SELECT EmpID, Name, id
FROM Users
WHERE (CAST(id AS Varchar(20)) IN ('5688','5689'))
现在如果我不从后面的代码传递参数,而不是相同的查询:字符串参数='5688','5689';它返回null。
Now if I do the same query passing the parameter instead from code behind: String param = "'5688','5689'"; it returns null.
WHERE (CAST(id AS Varchar(20)) IN (@param))
我试图起飞的第一个和最后一个,但是这并没有使探源。
I tried taking off the very first and last ', but that did not make a diffrence.
!!! id是一个独特的PK !!!
!!!id is a unique PK!!!
任何人有一个线索?
答
我找到解决的办法很简单,这就像一个魅力,而且也没有必要SPS或其他功能;
The solution I found is quite simple, this works like a charm and there's no need for sps or other functions;
SQL:
SELECT whatever
FROM whatever
WHERE (PATINDEX('%''' + CAST(id AS Varchar(20)) + '''%', @param) > 0)
C#:
String param = "'''1234'',''4567'''";
dataTable1 = tableAdapter1.getYourValues(param);