Using Dapper for parameterized queries

Source: Internet
Author: User

In the dapper operation MySQL database I described the use of dapper for Curd basic operation, but in the sample code, although the parameter is also through the @ start, but in fact is not the real meaning of the parameterized query, but splicing SQL, this method is not conducive to prevent SQL injection, So in Dappe, we can use dynamicparameters dynamic parameter collection to add parameters to realize parametric query under dapper;

Sample code

            using(varConnection =Newmysqlconnection (CONNSTR)) {                //declaring dynamic ParametersDynamicparameters Parameters =Newdynamicparameters (); //Start a transactionIDbTransaction transaction =connection.                BeginTransaction (); Try                {                    intCNT =0; stringSquery ="Delete from User"+"WHERE [email protected]"; //Adding ParametersParameters.Add ("Id", id); Connection. Execute (Squery, Parameters, transaction,NULL,NULL); //Commit a transactionTransaction.commit (); return true; }                Catch(Exception ex) {//exception occurred, transaction rollbacktransaction.                    Rollback (); return false; Throw NewException (ex.                Message); }            }

Using Dapper for parameterized queries

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.