如何在Kentico查询中使用可配置的Where子句

本文关键字:配置 Where 子句 Kentico 查询 | 更新日期: 2023-09-27 17:50:51

我当前将sql语句分配给一个NVarchar,然后执行它。但是我不喜欢这种工作方式。

DECLARE @sqlStatement NVARCHAR(4000)
SET @sqlStatement = N'
     SELECT *
     FROM CMS_User usr
     WHERE
     (    
          usr.LastName LIKE ''%' + @Search + '%'' OR
     )'
IF(@SearchWhereClause IS NOT NULL)
BEGIN
     SET @sqlStatement = @sqlStatement+  N' AND (' + @SearchWhereClause + ')'
END
SET @sqlStatement = @sqlStatement+  N' ORDER BY usr.LastName'
EXEC sp_executesql @statement = @sqlStatement

 QueryDataParameters parameters = new QueryDataParameters();
 parameters.Add("@Search", prefixText);
 //set configurabel searchWhereClause
 string searchWhereClause = SettingsKeyProvider.GetStringValue("WhereClause");
 if (string.IsNullOrEmpty(searchWhereClause))
 {
      searchWhereClause = null;
 }
 parameters.Add("@SearchWhereClause", searchWhereClause);
 DataSet ds = ConnectionHelper.ExecuteQuery("custom.DocType.Query", parameters);

如何在Kentico查询中使用可配置的Where子句

相反,您可以使用Kentico宏进行查询:

SELECT * 
FROM CMS_User usr
WHERE 
(   
    usr.LastName LIKE '%' + @Search + '%' OR 
)
AND ( usr.UserIsHidden IS NULL OR usr.UserIsHidden = 0 )
AND ( ##WHERE## )
ORDER BY usr.LastName

ConnectionHelper.ExecuteQuery(string queryName, QueryDataParameters parameters,
   string where);