有没有办法用 Dapper 调用存储过程?
stackoverflow.com 的 Dapper Micro ORM 的结果给我留下了深刻的印象。我正在考虑将其用于我的新项目,但我担心有时我的项目需要存储过程,并且我在网络上进行了很多搜索,但没有找到任何带有存储过程的内容。那么有没有办法让 Dapper 使用存储过程呢?
如果可能的话请告诉我,否则我必须以我的方式扩展它。
I am very impressed with the results of Dapper Micro ORM for stackoverflow.com. I am considering it for my new project and but I have one concern about that some times my project requires to have Stored Procedure and I have search a lot on web but not found anything with stored procedure. So is there any way to have Dapper work with a stored procedure?
Please let me know if it is possible otherwise I have to extend it in my way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在简单的情况下,你可以这样做:
如果你想要更奇特的东西,你可以这样做:
另外,你可以在批处理中使用 exec,但这更笨重。
In the simple case you can do:
If you want something more fancy, you can do:
Additionally you can use exec in a batch, but that is more clunky.
我认为答案取决于您需要使用存储过程的哪些功能。
返回结果集的存储过程可以使用
Query
运行;不返回结果集的存储过程可以使用Execute
来运行 - 在这两种情况下(使用EXEC
)作为 SQL 命令(加上输入参数作为必要的)。有关更多详细信息,请参阅文档。截至修订版2d128ccdc9a2似乎没有对 OUTPUT 参数的本机支持;您可以添加它,或者构造一个更复杂的
Query
命令,该命令声明TSQL变量,执行SP,将OUTPUT
参数收集到局部变量中,最后将它们返回到结果集中:I think the answer depends on which features of stored procedures you need to use.
Stored procedures returning a result set can be run using
Query
; stored procedures which don't return a result set can be run usingExecute
- in both cases (usingEXEC <procname>
) as the SQL command (plus input parameters as necessary). See the documentation for more details.As of revision 2d128ccdc9a2 there doesn't appear to be native support for
OUTPUT
parameters; you could add this, or alternatively construct a more complexQuery
command which declared TSQL variables, executed the SP collectingOUTPUT
parameters into the local variables and finallyreturned them in a result set:以下是从存储过程获取值返回的代码
存储过程:
代码:
Here is code for getting value return from Store procedure
Stored procedure:
Code:
与上面相同,更详细一点
使用.Net Core
控制器
存储过程(父子关系)
案例参考
Same from above, bit more detailed
Using .Net Core
Controller
Stored Procedure ( parent child relation )
References in case
具有多个返回和多个参数
With multiple return and multi parameter
这是带有参数的存储过程的代码
Here is code for stored procedure with parameters