Dapper Getting Started Tutorial (ii)--Executing non-query statements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.