繁体   English   中英

如何在Sql Server Compact Edition中使用LIKE参数

[英]How to use parameter with LIKE in Sql Server Compact Edition

我正在尝试参数化使用带有通配符的 LIKE 关键字的搜索查询。 原来的sql有这样的动态sql:

"AND JOB_POSTCODE LIKE '" + isPostCode + "%' "

所以我尝试了这个,但我得到了一个 FormatException:

"AND JOB_POSTCODE LIKE @postcode + '%' "

编辑:我猜 FormatException 不会来自 Sql Server CE,所以根据要求,这是我在 C# 代码中设置参数的方法。 该参数在代码中设置如下:

command.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = isPostCode;

我也试过:

"AND JOB_POSTCODE LIKE @postcode"

command.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = isPostCode + "%";

但这不会返回任何结果。 谁能建议如何在这个搜索sql中使用参数?

简短的回答是您应该将通配符放在参数的值中,而不是放在 CommandText 中。 IE

不是: sqlCommand.CommandText = "SELECT * FROM JOB WHERE JOB_POSTCODE LIKE @postcode%"

这个:

sqlCommand.CommandText = "SELECT * FROM JOB WHERE JOB_POSTCODE LIKE @postcode";
sqlCommand.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = postCode + "%";

长答案在这里:

我回过头来将我的代码精简为基本要素,以便我可以将其发布在这里,同时我发现我在原始问题中尝试的最后一种方法确实有效。 在我的测试中一定是出了什么问题。 所以这是一个摘要,其中包含已运行的完整代码:

原始动态sql,易受sql注入:

//Dynamic sql works, returns 2 results as expected, 
//but I want to use parameters to protect against sql injection

string postCode = "G20";
sqlCommand.CommandText = "SELECT * FROM JOB WHERE JOB_POSTCODE LIKE '" 
                         + postCode + "%'";
return Database.fGetDataSet(sqlCommand, 
                            iiStartRecord, 
                            iiMaxRecords, 
                            "JOBVISIT");

第一次尝试使用参数会出现错误:

//This syntax with a parameter gives me an error 
//(note that I've added the NVarChar length as suggested:
//System.FormatException : @postcode : G20 - 
//Input string was not in a correct format.
//at System.Data.SqlServerCe.SqlCeCommand.FillParameterDataBindings()
//at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor,
// Boolean& isBaseTableCursor)

string postCode = "G20";
sqlCommand.CommandText = "SELECT * FROM JOB WHERE JOB_POSTCODE LIKE @postcode 
                         + '%'";
sqlCommand.Parameters.Add("@postcode", 
                          SqlDbType.NVarChar, 
                          10).Value = postCode;
return Database.fGetDataSet(sqlCommand, iiStartRecord, iiMaxRecords, "JOBVISIT");

第二种技术确实有效:

///This syntax with a parameter works, returns 2 results as expected
string postCode = "G20";
sqlCommand.CommandText = "SELECT * FROM JOB WHERE JOB_POSTCODE LIKE @postcode";
sqlCommand.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = postCode 
                                                                   + "%";
return Database.fGetDataSet(sqlCommand, iiStartRecord, iiMaxRecords, "JOBVISIT");

感谢您的所有投入,并对最初的误导性问题表示抱歉...

这会在 SQL Server 05 中返回适当的结果(也可以在 SP 中运行),因此您尝试的最后一件事似乎应该有效。 但是我没有 Compact Edition 的测试台,所以也许这会有所作为(我很想知道是否是这样,以及为什么)。

declare @p1 nvarchar(50)
set @p1 = 'f'         -- initial value passed from your app
set @p1 = @p1 + '%'   -- edit value for use with LIKE
select * from JOB
where JOB_POSTCODE like @p1

编辑:

您使用的是什么版本的 SQLCE? 你能判断你的参数值是否真的从你的代码传递到数据库吗? 我浏览了 这个 MSDN 教程并且能够获得您正在寻找的结果,至少从 Visual Studio 查询设计器中获得。 (唯一的区别是我使用的是 VS 2008 和 SQL Server Compact 3.5)。 请参阅标题为“创建新查询”的部分; 我用一些数据模拟了一个表,这个查询按你的意图工作。

SELECT     JobID, PostCode, OtherValue
FROM         Job
WHERE     (PostCode LIKE @p1 + '%')

就像我说的,我没有编写任何代码来调用查询,但它在设计器内部工作。 换句话说,“它适用于我的机器”。

使用:

"AND JOB_POSTCODE LIKE '" + isPostCode + "%' "

...意味着字符串在被添加到动态 SQL 之前被构造。 这样您就不必在 sp_executesql/EXEC 的参数列表中指定参数,而这样做:

"AND JOB_POSTCODE LIKE @postcode + '%' "

...做。 请发布更多查询。

请发布您的完整示例(包括您在其中组装呼叫的外部客户端代码)。 如果您正确传递参数,您的第二个和第三个选项都应该有效。 您是在存储过程还是内联参数化 SQL 中调用它?

我假设没有 SP,因为我只是看到您正在使用 CE...

我认为您需要为.Add调用添加一个长度,因为它是一个nvarchar

select province_address 
from address 
where (@postcode is null or postcode like '%' + @postcode '%')

如果你这样使用,这意味着如果你没有在@postcode 中发送任何值,它将带来所有,否则它只会带来包含@postcode

你的回答是:

"AND JOB_POSTCODE LIKE '' + @postcode + '%' "

和参数:

command.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = isPostCode;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM