精致的订购方式
使用 dapper 时是否有任何原因无法以正确的顺序检索以下代码?
connection.Query<User>("SELECT id, name " +
"FROM user " +
"ORDER BY @sort @dir " +
"LIMIT @offset, @pageSize; ",
new {
sort = sortOrder, // sortOrder = "name"
dir = sortDirection, // sortDirection = "ASC"
offset = pageIndex * pageSize, // offset = 0
pageSize = pageSize // pageSize = 10
});
它总是返回而不应用排序。
我可以像这样将 sortOrder 和 sortDirection 直接放入字符串中
"SELECT id, name " +
"FROM user " +
"ORDER BY " + sortOrder + " " + sortDirection + " " +
"LIMIT @offset, @pageSize; "
,但我不确定这会如何影响 dapper,因为我相信它有自己的查询计划缓存。
另外,有没有办法查看 dapper 生成的查询?
Is there any reason why the following code wouldn't be retrieved in the correct order when using dapper?
connection.Query<User>("SELECT id, name " +
"FROM user " +
"ORDER BY @sort @dir " +
"LIMIT @offset, @pageSize; ",
new {
sort = sortOrder, // sortOrder = "name"
dir = sortDirection, // sortDirection = "ASC"
offset = pageIndex * pageSize, // offset = 0
pageSize = pageSize // pageSize = 10
});
It always returns without applying the ordering.
I could just place the sortOrder and sortDirection directly into the string like this
"SELECT id, name " +
"FROM user " +
"ORDER BY " + sortOrder + " " + sortDirection + " " +
"LIMIT @offset, @pageSize; "
but I'm not sure how that will effect dapper since I believe it has it's own query plan caching.
Also, is there a way to view the query that is generated by dapper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,一般数据库引擎不允许您参数化列名称。例如:
当您尝试参数化 order by 子句时,我建议使用内联替换,前提是您可以保证转义,bobby 表始终潜伏着。 (或者你可以使用一个过程)
我不确定(MySQL?Oracle?)的分析器的情况,但你可以使用像 MiniProfiler 查看 SQL。
它会影响缓存,但是 sortOrder 和 sortDirection 的排列数量很少,因此影响很小。
Sure, in general database engines do not allow you to parametrize column names. So for example:
When you are trying to parametrize order by clauses I would recommend using inline substitution, provided you can guarantee your escaping, bobby tables is always lurking. (Either that or you could use a proc)
I am not sure about the situation around profilers for (MySQL? Oracle?) but you could use a tool like MiniProfiler to see the SQL.
It will affect caching, however there are only a small number of permutations of sortOrder and sortDirection so the impact is minimal.
您可以在订单中使用 case 语句。只要您只处理几列,就不会变得太疯狂。
You can just use a case statement in your order by. As long as you're only dealing with a few columns, it won't get too crazy.