Describe
You can call Dapper's extension method "Execute" from any class object that implements IDbConnection. It can execute a command one or more times and return the number of rows affected. This method is typically used to perform:
- Stored Procedure
- INSERT statement
- UPDATE statement
- DELETE statement
Parameters
The following table shows the different parameters of the Execute method
name |
Description |
Sql |
The text of the SQL statement to execute |
Param |
Command's parameters |
Transaction |
Transaction |
CommandTimeout |
Command time-out time |
CommandType |
Command type |
Example: Executing a stored procedure single time
Execute the stored procedure once
string " EXEC Invoice_insert " ; using (var connection = my.connectionfactory ()) { connection. Open (); var affectedrows = connection. Execute (SQL, new"single_insert_1"}, CommandType:CommandType.StoredProcedure); My.Result.Show (affectedrows);}
Times
Executes the stored procedure multiple times, and each data in the array is executed once
stringsql ="EXEC Invoice_insert";using(varConnection =my.connectionfactory ()) {connection. Open (); varAffectedRows =connection. Execute (SQL,New[] { New{Kind = Invoicekind.webinvoice, Code ="many_insert_1"}, New{Kind = Invoicekind.webinvoice, Code ="many_insert_2"}, New{Kind = Invoicekind.storeinvoice, Code ="Many_insert_3"}}, CommandType:CommandType.StoredProcedure); My.Result.Show (affectedrows);}
Example: Performing an insert single-time
Execute INSERT statement once
string " INSERT into Invoice (Code) Values (@Code); " ; using (var connection = my.connectionfactory ()) { connection. Open (); var New " single_insert_1 " }); My.Result.Show (affectedrows);}
Times
Executes multiple times, and each data in the array is executed once
stringsql ="INSERT into Invoice (Code) Values (@Code);";using(varConnection =my.connectionfactory ()) {connection. Open (); varAffectedRows =connection. Execute (SQL,New[] { New{Kind = Invoicekind.webinvoice, Code ="many_insert_1"}, New{Kind = Invoicekind.webinvoice, Code ="many_insert_2"}, New{Kind = Invoicekind.storeinvoice, Code ="Many_insert_3"} } ); My.Result.Show (affectedrows);}
Example: Performing an update single time
Execute UPDATE statement once
string " UPDATE Invoice SET Code = @Code WHERE Invoiceid = @InvoiceID " ; using (var connection = my.connectionfactory ()) { connection. Open (); var New 1 " Single_update_1 " }); My.Result.Show (affectedrows);}
Times
Executes multiple times, and each data in the array is executed once
stringsql ="UPDATE Invoice SET Code = @Code WHERE Invoiceid = @InvoiceID";using(varConnection =my.connectionfactory ()) {connection. Open (); varAffectedRows =connection. Execute (SQL,New[] { New{Invoiceid =1, Code ="Many_update_1"}, New{Invoiceid =2, Code ="many_update_2"}, New{Invoiceid =3, Code ="Many_update_3"} }); My.Result.Show (affectedrows);}
Example: Executing a delete single time
Execute the DELETE statement once
string " DELETE from Invoice WHERE Invoiceid = @InvoiceID " ; using (var connection = my.connectionfactory ()) { connection. Open (); var New 1 }); My.Result.Show (affectedrows);}
Times
Executes multiple times, and each data in the array is executed once
stringsql ="DELETE from Invoice WHERE Invoiceid = @InvoiceID";using(varConnection =my.connectionfactory ()) {connection. Open (); varAffectedRows =connection. Execute (SQL,New[] { New{Invoiceid =1}, New{Invoiceid =2}, New{Invoiceid =3} });}
Dapper Getting Started Tutorial (ii)--Executing non-query statements