简体   繁体   English

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

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

I'm trying to parameterise a search query that uses the LIKE keyword with a wildcard.我正在尝试参数化使用带有通配符的 LIKE 关键字的搜索查询。 The original sql has dynamic sql like this:原来的sql有这样的动态sql:

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

So I've tried this instead, but I get a FormatException:所以我尝试了这个,但我得到了一个 FormatException:

"AND JOB_POSTCODE LIKE @postcode + '%' "

Edit: I guess the FormatException isn't going to be coming from Sql Server CE, so as requested, here is how I set the parameter in my C# code.编辑:我猜 FormatException 不会来自 Sql Server CE,所以根据要求,这是我在 C# 代码中设置参数的方法。 The parameter is set in code like this:该参数在代码中设置如下:

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

I also tried:我也试过:

"AND JOB_POSTCODE LIKE @postcode"

with

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

but that doesn't return any results.但这不会返回任何结果。 Can anyone advise how to use parameters in this search sql?谁能建议如何在这个搜索sql中使用参数?

The short answer is that you should put the wildcard in the Value of the parameter, not in the CommandText.简短的回答是您应该将通配符放在参数的值中,而不是放在 CommandText 中。 ie IE

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

this:这个:

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

Long answer here:长答案在这里:

I went back and stripped my code down to the essentials so that I could post it here, and while doing that I discovered that the last method I tried in my original question does actually work.我回过头来将我的代码精简为基本要素,以便我可以将其发布在这里,同时我发现我在原始问题中尝试的最后一种方法确实有效。 Must have been something wrong in my testing.在我的测试中一定是出了什么问题。 So here's a summary, with full code that's been run:所以这是一个摘要,其中包含已运行的完整代码:

Original dynamic sql, vulnerable to sql injection:原始动态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");

First attempt to use parameter gives an error:第一次尝试使用参数会出现错误:

//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");

Second technique does actually work:第二种技术确实有效:

///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");

Thanks for all the input, and sorry about the original misleading question...感谢您的所有投入,并对最初的误导性问题表示抱歉...

This returns appropriate results in SQL Server 05 (also works wrapped in a SP), so it seems like the last thing you tried should have worked.这会在 SQL Server 05 中返回适当的结果(也可以在 SP 中运行),因此您尝试的最后一件事似乎应该有效。 But I don't have a test bed for Compact Edition, so maybe that makes a difference (I'd be curious to see if that's so, and why).但是我没有 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

EDIT:编辑:

What version of SQLCE are you using?您使用的是什么版本的 SQLCE? Can you tell if your parameter values are actually being passed from your code to the DB?你能判断你的参数值是否真的从你的代码传递到数据库吗? I skimmed through this MSDN tutorial and was able to get the results you're looking for, at least from the Visual Studio Query Designer.我浏览了 这个 MSDN 教程并且能够获得您正在寻找的结果,至少从 Visual Studio 查询设计器中获得。 (only difference is that I'm using VS 2008 and SQL Server Compact 3.5). (唯一的区别是我使用的是 VS 2008 和 SQL Server Compact 3.5)。 See the section titled "Creating a new query";请参阅标题为“创建新查询”的部分; I mocked up a table with some data and this query worked as you intend.我用一些数据模拟了一个表,这个查询按你的意图工作。

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

Like I said, I didn't write any code to call the query, but it worked from within the designer.就像我说的,我没有编写任何代码来调用查询,但它在设计器内部工作。 In other words, "it works on my machine".换句话说,“它适用于我的机器”。

Using:使用:

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

...means that the string is constructed before being tacked onto the dynamic SQL. ...意味着字符串在被添加到动态 SQL 之前被构造。 This is so that you don't have to specify the parameter in the parameter list for sp_executesql/EXEC, while this:这样您就不必在 sp_executesql/EXEC 的参数列表中指定参数,而这样做:

"AND JOB_POSTCODE LIKE @postcode + '%' "

...does. ...做。 Please post more of the query.请发布更多查询。

Please post your complete example (including the outer client code where you are assembling your call).请发布您的完整示例(包括您在其中组装呼叫的外部客户端代码)。 Both your second and third options should work if you are passing the parameter correctly.如果您正确传递参数,您的第二个和第三个选项都应该有效。 Are you calling this in a stored procedure or inline parameterized SQL?您是在存储过程还是内联参数化 SQL 中调用它?

I assume no SPs since I just see you are using CE...我假设没有 SP,因为我只是看到您正在使用 CE...

I think you need to add a length to your .Add call since it's an nvarchar .我认为您需要为.Add调用添加一个长度,因为它是一个nvarchar

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

if you use like this it means if you not sent any value in @postcode it will bring all otherwise it will bring just include @postcode如果你这样使用,这意味着如果你没有在@postcode 中发送任何值,它将带来所有,否则它只会带来包含@postcode

Your answer is:你的回答是:

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

and parameter:和参数:

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

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

相关问题 如何在SQL Server Compact Edition中保存小数? - How to save decimals in SQL Server Compact Edition? 如何使用应用程序部署.Net Compact Framework和SQL Server Compact Edition - How to deploy the .Net Compact Framework and SQL Server Compact Edition with the Application SQL Server Compact Edition无服务器吗? - Is SQL Server Compact Edition Serverless? SQL Server Compact Edition 4 - AccessViolationException - SQL Server Compact Edition 4 - AccessViolationException 在ReportViewer .Net 4.0中使用Sql Server Compact Edition数据库文件 - Use Sql Server Compact Edition Database File in ReportViewer .Net 4.0 如何在Windows 64位上使用SQL Server Compact Edition 32位? - How to use SQL Server Compact Edition 32bits on Windows 64bit? 如何使用SQL Server Compact Edition作为桌面应用程序中的嵌入式源以及如何在应用程序本身中进行bakup? - How to use SQL Server Compact edition as embedded source in desktop application and also to bakup in application itself? 在SQL Server Compact Edition中可以进行批量查询吗? - Is batch query possible in SQL Server Compact Edition? 具有实体框架的SQL Server Compact Edition - SQL Server Compact Edition with Entity Framework SQL Server可以复制到SQL Server Compact Edition吗? - Can SQL Server replicate TO SQL Server Compact Edition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM