簡體   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