2016-04-27 46 views
0

對於應用程序,我正在.NET中運行DocumentDb上的查詢。對於這個曾經我想用一個參數化查詢,例如:使用參數查詢查詢.NET中的DocumentDb

var sqlString = "select p.Id, p.ActionType, p.Type, p.Region, a.TimeStamp, a.Action from History p join a in p.Actions where a.TimeStamp >= @StartTime and a.TimeStamp <= @EndTime and p.ClientId = @ClientId and p.ActionType = @ActionType"; 
if (actionType != "") { sqlString += actionTypeFilter; } 

var queryObject = new SqlQuerySpec 
{ 
    QueryText = sqlString, 
    Parameters = new SqlParameterCollection() 
    { 
     new SqlParameter("@StartTime", startDate), 
     new SqlParameter("@EndTime", endDate), 
     new SqlParameter("@ClientId", clientId.ToString()), 
     new SqlParameter("@ActionType", actionType) 
    }, 
}; 

var dataListing = _documentDbClient.CreateDocumentQuery<PnrTransaction>(UriToPnrHistories, queryObject, new FeedOptions() { MaxItemCount = 1 }); 

當我執行此,我越來越EN空數據集。但是,當我使用相同的查詢,並使用經典字符串替換它,它工作得很好。

任何人都可以告訴我我在參數化查詢中做錯了什麼嗎?

回答

0

如果上面的代碼是正在運行的代碼,那麼您仍然在參數化的SQL字符串上添加actiontypeFilter。嘗試刪除第2行中的if語句。對我來說可能是您的問題。

如果您可以從服務器發佈示例文檔,這將有所幫助。

+0

根據問題發表澄清意見,而不是回答。 –

+1

「你必須有50個評論的聲望」......正在處理它。 – hsulriksen

0

我通常看到這種語法:

SqlParameterCollection parameters = new SqlParameterCollection(); 
parameters.Add(...); 
parameters.Add(...); 

試一下,看看你得到不同的結果。可能是因爲您的答案中用於初始化它的列表需要輸入不同的工作方式。