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 has great performance because it doesn't translate queries that we write in . NET to SQL. It is important to know that Dapper is SQL Injection safe because we can use parameterized queries, and that's something we should always do. One more important thing is that Dapper supports multiple database providers.
Dapper also provides a DynamicParameters class, which represents a "bag" of parameter values. You can pass an object to its constructor. Suitable objects include a Dictionary<string, object> : var dictionary = new Dictionary<string, object>
GitHub - sqlkata/querybuilder: SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird.
Dapper provides the Execute method (and its async equivalent) for commands that are not intended to return resultsets i.e. INSERT , UPDATE and DELETE commands. The Execute method returns an int , representing the number of rows affected by the successful completion of the command.
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 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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With