Basic use of Dapper.extension

Source: Internet
Author: User

Objective

The previous essay wrote the simple use of dapper, This time to write about the use of dapper.extension, it is a simple package extension of dapper, can be instantiated by the object assignment after the operation of adding and removing, and paging, but can not do multi-table query operation, this needs to expand it yourself. The operation of the transaction is the same as the dapper.

1. Get a single entity  

  This is a straightforward pass-through ID to get the returned object, as follows:

 public   Student Extget () {  using  (IDbConnection conn = new  Span style= "COLOR: #000000" > SqlConnection (SqlConnection)) { return  Conn. Get<student> (1 ); //  here can only pass ID query, and Student fields must match the table field, one less one will be an error. So getting a single object feels like a dapper native statement. Good  
   
    // 
    return Conn. Get<student> ("Select Id,score,courseid,studentid from Student where id = 1");  
    // 
     }}  
   

2. Get multiple Entities List

The specific code and comments are as follows:

         PublicList<student>extlist () {using(IDbConnection conn =NewSqlConnection (SqlConnection)) {IList<ISort> sortlist =NewList<isort>(); Sortlist. ADD (NewSort {propertyname ="ID", ascending =false});//Sorting CriteriaIList<IPredicate> prelist =NewList<ipredicate>(); Prelist.add (Predicates.field<Student> (o = o.id, Operator.eq,2));//search criteria, there are many types of operator such as EQ equals, like usage for SQL and LT, Le, etc.betweenvalues betweenvalues=NewBetweenvalues ();//search criteria, between search for data between two valuesBetweenvalues.value1 =1; Betweenvalues.value2=6; Prelist.add (Predicates.between<Student> (o =o.id, betweenvalues)); varSubpre = predicates.field<student> (o = o.id, Operator.eq,3);//search criteria to determine if a condition exists, followed by a Boolean argument if True indicates not present, false is presentPrelist.add (predicates.exists<student> (Subpre,true)); Prelist.add (Predicates.property<Student,Student> (o = o.id, operator.eq, t = t.id));//determine the relationship of two fields, such as whether equal, greater than, less than, and field is similar, and can not be used for two table field judgmentIpredicategroup Predgroup= Predicates.group (groupoperator.or, Prelist.toarray ());//confirm how multiple search conditions are connected and or                varList = conn. Getlist<student> (Predgroup, sortlist). Tolist<student>(); returnlist; //the SQL statements generated by the above code are as follows//exec sp_executesql N ' SELECT [Student]. [id], [Student]. [Name], [Student]. [Sex], [Student]. [Tel] From [Student]//WHERE ([student].[ ID] = @id_0)//OR//([student].[ ID] between @id_1 and @id_2)//OR//(Not EXISTS (SELECT 1 from [Student] WHERE ([student].[ ID] = @id_3 )))//OR//([student].[ ID] = [Student]. [id]))//ORDER by [Student]. [id] DESC ', N ' @id_0 int, @id_1 int, @id_2 int, @id_3 int ', @id_0 =2, @id_1 =1, @id_2 =6, @id_3 =3            }        }

3. Paging

Pagination specific and GetList method is the same, just a number of page numbers and data per page parameters, the specific code is as follows:

         PublicList<student>extpagelist () {using(IDbConnection conn =NewSqlConnection (SqlConnection)) {IList<ISort> sortlist =NewList<isort>(); Sortlist. ADD (NewSort {propertyname ="ID", ascending =false});//Sorting CriteriaIList<IPredicate> prelist =NewList<ipredicate>(); Prelist.add (Predicates.field<Student> (o = o.id, Operator.eq,2)); Ipredicategroup Predgroup=Predicates.group (groupoperator.or, Prelist.toarray ()); returnConn. Getpage<student> (Predgroup,sortlist,1,Ten).            ToList (); }        }

4. Add

        /// <summary>        ///added, can only be added individually///by passing in the assembled class object, you can insert the data///If no data is null or is the default value for a database field/// </summary>         Public voidExtadd () {using(IDbConnection conn =NewSqlConnection (SqlConnection)) {Student Student=NewStudent (); Student.name="Test Students"; Student.sex=1; //student.tel= "13222222222";                intID = conn. Insert<student> (Student);//returns the self-increment primary key ID            }        }

5. Update

        /// <summary>        ///updates, only single updates///If a field is not assigned, it causes the database field to be updated to null.///so it's important to keep the data intact./// </summary>         Public voidextupdate () {using(IDbConnection conn =NewSqlConnection (SqlConnection)) {Student Student=NewStudent (); Student.id=1013; Student.name="Test Students"; Student.sex=1; //student.tel= "13222222222";                if(Conn. Update<student>(student)) {                    //Success                }                Else                 {                    //failed                }            }        }

6. Delete

        /// <summary>        ///Delete, only single delete///this way, you can specify the criteria for deletion by passing in an ID entity or predicates object .///predicates words and list that part of the operation is the same, you can refer to the extlist kind of detailed use/// </summary>         Public voidExtdel () {using(IDbConnection conn =NewSqlConnection (SqlConnection)) {Student Student=NewStudent (); Student.id=1020; varPre = predicates.field<student> (o = o.id, Operator.eq,1017); if(Conn. Delete<student> (Student))//or Conn. Delete<student> (PRE)                {                    //Success                }                Else                 {                    //failed                }            }        }

Basic use of Dapper.extension

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.