Dapper Study-Dapper.rainbow (iii)-Read

Source: Internet
Author: User

The new/modified/deleted is described earlier, and the following is a rainbow Read method.

First, Read--Rainbow native

1. See the Test Code First

varConstr = configurationmanager.connectionstrings["Cons"]. ToString ();using(varconn =Newmysqlconnection (CONSTR)) {    vardb = Rainbow.init (conn, -); //Rainbow provides methods    varentity = db. Teacher.get (8); Entity=db.    Teacher.first (); varListall =db. Teacher.all ();}

Rainbow in reading the data this piece, just provide these methods, of course, dapper method, here is still can be used, through the DB. You can use the Query method.

2. Source code Parsing

/// <summary>///Grab a record with a particular Id from the DB/// </summary>/// <param name= "id" ></param>/// <returns></returns> PublicT Get (TId id) {returnDatabase. Query<t> ("SELECT * from"+ TableName +"where"+"Id"+"= @id",New{ID}). FirstOrDefault ();} Public VirtualT First () {returnDatabase. Query<t> ("SELECT * from"+ TableName +"LIMIT 1;").    FirstOrDefault (); //return database. Query<t> ("SELECT top 1 * from" + TableName). FirstOrDefault ();} PublicIenumerable<t>All () {returnDatabase. Query<t> ("SELECT * from"+TableName);}

The first method needs to be modified to accommodate MySQL. However, the first method is a virtual method, in the absence of source modification, you can use the class to inherit the rewrite can be, it is more convenient. If you have the source, it's more convenient.

Here are some of the packages are very simple, near to the naked SQL, not much to say

Ii. Custom Extensions

1. Test the code:

varConstr = configurationmanager.connectionstrings["Cons"]. ToString ();using(varconn =Newmysqlconnection (CONSTR)) {    vardb = Rainbow.init (conn, -); //Custom extension Methods    varList = db. Teacher.getlistby ("no> @No",New{No =6060 }); varPageList = db. Teacher.getpagelist (1,Ten,"no> @No",New{No =6060 });}

After testing, it can be used normally. Next look at the code

2. Custom Methods

/// <summary>///Query by Condition/// </summary>/// <param name= "Wherestr" >without the WHERE keyword</param>/// <returns></returns> PublicIenumerable<t> Getlistby (stringWHERESTR,Dynamicparam =NULL){    stringsql ="SELECT * from"+TableName; if(!string. Isnullorwhitespace (WHERESTR)) SQL+="where"+Wherestr; returnDatabase. Query<t>(SQL, param);}

Here the method, only applies to a single table, for multi-table queries, you can directly use the dapper method, write SQL.

/// <summary>///Pagination Method/// </summary>/// <param name= "PageIndex" >Current page number</param>/// <param name= "PageSize" >show number of bars per page</param>/// <param name= "WhereCondition" >Where Condition</param>/// <param name= "param" >List of parameters in the Where Condition</param>/// <param name= "Orderbyproperty" >sort fields, such as name DESC or ID desc,name ASC</param>/// <returns></returns> Public VirtualPagedresult<t> Getpagelist (intPageIndex,intPageSize,stringWhereCondition ="",Dynamicparam =NULL,stringOrderbyproperty =""){    if(PageIndex <1) PageIndex =1; varStartItem = (PageIndex-1) *pageSize; string where=string. IsNullOrEmpty (wherecondition)?"":"WHERE"+WhereCondition; string  by=string. IsNullOrEmpty (orderbyproperty)?"":"ORDER by"+Orderbyproperty; stringsql =string. Format (@"SELECT * from {0} {1} {2} LIMIT {3},{4}; SELECT COUNT (1) from {0} {1}", TableName,where, by, StartItem, pageSize); Pagedresult<T> Pagingresult =NewPagedresult<t>(PageSize, PageIndex); using(varresult =database. Querymultiple (SQL, param)) {List<T> list = result. Read<t>(); varTotalCount = Convert.ToInt32 (result. read<Long> () [0]); Pagingresult.data=list; Pagingresult.totalitemcount=TotalCount; }    returnPagingresult;}

This is a single-table paging method, if you want to multi-table, only need to slightly modify the next method, you can. Don't put it on.

Here is involved in a querymultiple method, is not mentioned in the previous dapper parsing space, this method is a sub-query, if the SQL statement is a concatenation of multiple SQL statements, then you can read multiple times to get more than one query result

Dapper Study-Dapper.rainbow (iii)-Read

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.