必须声明标量变量-Dapper

时间:2019-01-16 08:34:36

标签: c# dapper

我在小包装上进行了多次映射。然后我尝试实现dapper构建器

但是它返回异常:

Must declare the scalar variable \"@ExecutionId\".\r\nInvalid usage of the option NEXT in the FETCH statement.

没有多重映射,永远不会出问题

这是我的代码段

var Builder = new SqlBuilder();

var SelectedQuery = Builder.AddTemplate(@ "SELECT e.[Id], e.[BuyOrderBookId], e.[SellOrderBookId], e.[Volume], e.[Price], e.[CreationDate], e.[StatusId], bo.[UserId], bo.[MarketId], so.[UserId] FROM[dbo].[Execution] AS e JOIN[dbo].[OrderBook] AS bo ON e.BuyOrderBookId = bo.Id JOIN[dbo].[OrderBook] as so ON e.SellOrderBookId = so.Id
  /**where**/
  ORDER BY e.[CreationDate] DESC OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY;
  ");

  //Execution ID
  if (filter.ExecutionId.HasValue)
   Builder.Where("e.[Id] = @ExecutionId", new {ExecutionId = filter.ExecutionId.Value
   });

  var query = await connection.QueryAsync < ExecutionViewModel, OrderBookViewModel, OrderBookViewModel, ExecutionViewModel > (SelectedQuery.RawSql, (execute, buyOrder, sellOrder) => {
    execute.BuyUserId = buyOrder.UserId;
    execute.SellUserId = sellOrder.UserId;
    execute.MarketId = buyOrder.MarketId;
    return execute;
   },    
   splitOn: "UserId,UserId",
   param: new {
    SelectedQuery.Parameters,
     skip = (pagingParam.PageNumber - 1) * pagingParam.PageSize,
     take = pagingParam.PageSize
   });

有人知道我在这里做错了吗?

更新

我只是这样解决

if (filter.ExecutionId.HasValue)
                Builder.Where(String.Format("e.[Id] = {0}",filter.ExecutionId));

我认为这不是实现的好方法。是风险sql注入。

2 个答案:

答案 0 :(得分:0)

尝试像这样更改where子句:

if (filter.ExecutionId.HasValue)
   Builder.Where("e.[Id]", new {Id = filter.ExecutionId.Value});

答案 1 :(得分:0)

您可以添加这样的参数。

if (filter.ExecutionId.HasValue)
{
    Builder.Where("e.[Id] = @ExecutionId");
    ((DynamicParameters)SelectedQuery.Parameters)
        .AddDynamicParams(new {
             ExecutionId = filter.ExecutionId.Value
         });
 }