错误:
System.Data.SqlClient.SqlException:','
附近的语法不正确
代码:
IQuery permissionTypes;
if (regionIds != null && regionIds.Count > 0)
{
permissionTypes = session.CreateSQLQuery(
@"SELECT DISTINCT PT.PermissionType FROM AspNetUsers AU
INNER JOIN AspNetUserRoles AR ON AU.Id= AR.UserId
INNER JOIN dbo.AspNetRoles ANR ON ANR.Id = AR.RoleId
INNER JOIN [dbo].[tbl_RolePermissionTypeAssoc] RPT ON RPT.FK_RoleId = AR.RoleId
INNER JOIN tbl_PermissionType PT ON RPT.FK_permissionTypeId=PT.Id
WHERE AU.ID=:userId AND ANR.RegionId=:regionId")
.SetParameter("userId", userId).SetParameterList("regionId", regionIds);
}
else
{
permissionTypes = session.CreateSQLQuery(
@"SELECT DISTINCT PT.PermissionType FROM AspNetUsers AU
INNER JOIN AspNetUserRoles AR ON AU.Id= AR.UserId
INNER JOIN dbo.AspNetRoles ANR ON ANR.Id = AR.RoleId
INNER JOIN [dbo].[tbl_RolePermissionTypeAssoc] RPT ON RPT.FK_RoleId = AR.RoleId
INNER JOIN tbl_PermissionType PT ON RPT.FK_permissionTypeId=PT.Id
WHERE AU.ID=:userId AND ANR.RegionId=:regionId")
.SetParameter("userId", userId);
}
return permissionTypes.List<string>();
生成的查询:
SELECT DISTINCT PT.PermissionType
FROM AspNetUsers AU
INNER JOIN AspNetUserRoles AR ON AU.Id = AR.UserId
INNER JOIN dbo.AspNetRoles ANR ON ANR.Id = AR.RoleId
INNER JOIN [dbo].[tbl_RolePermissionTypeAssoc] RPT ON RPT.FK_RoleId = AR.RoleId
INNER JOIN tbl_PermissionType PT ON RPT.FK_permissionTypeId = PT.Id
WHERE AU.ID = @p0 AND ANR.RegionId = @p1, @p2
答案 0 :(得分:2)
这里的要点是 =
符号,它不能用于更多参数。我们需要 IN
运营商
resluting sql contains
AND ANR.RegionId = @p1, @p2
并且错误中报告了此昏迷。我们需要:
AND ANR.RegionId IN (@p1, @p2)
if
部分
session.CreateSQLQuery(
@"SELECT DISTINCT PT.PermissionType FROM AspNetUsers AU
INNER JOIN AspNetUserRoles AR ON AU.Id= AR.UserId
INNER JOIN dbo.AspNetRoles ANR ON ANR.Id = AR.RoleId
INNER JOIN [dbo].[tbl_RolePermissionTypeAssoc] RPT
ON RPT.FK_RoleId = AR.RoleId
INNER JOIN tbl_PermissionType PT ON RPT.FK_permissionTypeId=PT.Id
// this is wrong
// WHERE AU.ID=:userId AND ANR.RegionId=:regionId
// we need IN
WHERE AU.ID=:userId AND ANR.RegionId IN (:regionId)
")
.SetParameter("userId", userId)
.SetParameterList("regionId", regionIds);
而且在其他部分,我们正在使用未通过的参数
else
部分:
session.CreateSQLQuery(
@"SELECT DISTINCT PT.PermissionType FROM AspNetUsers AU
INNER JOIN AspNetUserRoles AR ON AU.Id= AR.UserId
INNER JOIN dbo.AspNetRoles ANR ON ANR.Id = AR.RoleId
INNER JOIN [dbo].[tbl_RolePermissionTypeAssoc] RPT ON RPT.FK_RoleId = AR.RoleId
INNER JOIN tbl_PermissionType PT ON RPT.FK_permissionTypeId=PT.Id
-- here is again RegionId, but that is not passed
WHERE AU.ID=:userId AND ANR.RegionId=:regionId")
.SetParameter("userId", userId);
预计会再次发生RegionId但未通过