本文介绍了比较Dapper中的QUERY和EXECUTE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下在使用DAPPER插入,更新,删除,删除,查询特定数据时,最好使用什么?我真的对在DAPPER中使用EXECUTE和QUERY命令感到困惑。

I would like to ask if what is the best to use when INSERTING, UPDATING, DELETING, REMOVING, QUERY SPECIFIC DATA using DAPPER? I'm really confused in using EXECUTE and QUERY command in DAPPER..

推荐答案

这根本不会造成混淆,尤其是在您可以查看Dapper公开的方法的签名(根据文档):

This should not be confusing at all, especially if you look at the signature of the methods exposed by the Dapper (as per documentation):

public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

Query方法专门用于内部执行select语句,该语句可以返回类型为T 的 IEnumerable,有执行它的选项。使用匿名参数,那么您不会期望任何返回值 Output参数,它只接受输入参数并提供结果,该结果具有与 Type T 。如果需要返回值输出参数,则需要使用 DynamicParameters进行绑定

Query method is specifically meant for executing a select statement internally, which can return the IEnumerable of a type T, there are options to execute it, if done using anonymous parameter, then you are not expecting any return value or Output parameter, it just takes input parameter and provide the result, which has schema matching to the properties of Type T. In case return value or Output parameter is required, then that needs to be bound using DynamicParameters

public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)

Execute方法用于执行DML语句,例如 Insert ,更新和删除,其目的是对数据库中的数据进行更改。返回类型是整数,如果在SQL Server中我们将 Set RowCount On ,该调用将无助于返回结果集,仅适用于DML调用。

Execute method is meant for executing the DML statements, like Insert, Update and Delete, whose purpose is to make changes to the data in the database. The return type is an integer, which should contain the value of number of rows updated, if in SQL Server we have set Set RowCount On, this call will not help in returning the Result Set, its only for DML calls.

如果您需要多个结果集,则我们有 QueryMultiple 。它返回一个 GridReader ,并且可以使用MARS(多个活动结果集)的概念来返回多个Select语句的结果。

In case you need multiple result set then we have QueryMultiple. which returns a GridReader and can be used to return the result of multiple Select statements, using a concept of MARS (Multiple active result set).

实际上,如果您的目标只是执行一个过程,它们中的任何一个都可以执行,但是更重要的是,期望结果集是什么,它们都有不同的回报来提供结果

Practically if your aim is just to execute a procedure, any of them would do, but what is more important is what result set are looking forward to receive, they all have different return to provide results

这篇关于比较Dapper中的QUERY和EXECUTE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:13