Notes on the use of dapper 1

Source: Internet
Author: User

1. Execute a query and map the results to a strongly Typed Listnote:all extension methods assume the connection is already open, they would fail if the connection is CLOSED.PU Blic static ienumerable<t> query<t> (this idbconnection CNN, string sql, object param = null, sqltransaction TR Ansaction = NULL, BOOL buffered = TRUE) Example usage:
 Public classdog{ Public int? Age {Get;Set; }  PublicGuid Id {Get;Set; }  Public stringName {Get;Set; }  Public float? Weight {Get;Set; }  Public intIgnoredproperty {Get{return 1; } }}             varGUID =Guid.NewGuid ();varDog = connection. Query<dog> ("Select age = @Age, Id = @Id",New{age = (int?)NULL, Id =guid}); Dog. Count (). Isequalto (1);d og. First (). Age.isnull ();d og. First (). Id.isequalto (GUID);

2. Execute a query and map it to a list of dynamic obj ECTs
 Public StaticIenumerable<dynamic> Query ( ThisIDbConnection CNN,stringSqlObjectparam =NULL, sqltransaction transaction =NULL,BOOLBuffered =true) This method would execute SQL andreturna dynamic list. Example Usage:varrows = connection. Query ("Select 1 A, 2 B UNION ALL select 3, 4"); ((int) rows[0]. A). Isequalto (1);((int) rows[0]. B). Isequalto (2);((int) rows[1]. A). Isequalto (3);((int) rows[1]. B). Isequalto (4); DbConnection Con= This. Getconnection ();returnCon. Query<t_so> ("SELECT * from Dbo.t_so"). ToList ();//can be like above, support parametric query, straightforward! 

3. Execute a Command that returns no results
 Public Static int Execute (Thisstringobjectnullnull) Example usage:connection. Execute (@ "  SET NOCOUNT on   CREATE TABLE #t (i int)   SET NOCOUNT off   insert #t   Select @a a UNION ALL select @b   set NOCOUNT on   drop table #t"new {a=1, b=2  })   . Isequalto (2);

To insert an object into the data:

T_so so =NewT_so (); Keyseq=Guid.NewGuid (); SoNo= This. Ssono.text;so. Sodate= Getvalue.getdatetime ( This. ssodate.text); so. CustomerName= This. Scustomername.text;so. Remark= This. Sremark.text;stringstrSQL =@"INSERT into Dbo.t_so (Keyseq,sono,sodate,customername,remark) VALUES (@KeySeq, @SoNo, @SoDate, @CustomerName, @ Remark)";D bconnection Con= This. Getconnection ();//con. Execute (strSQL, new {keyseq = so. Keyseq, SoNo = so. SoNo, sodate = so. Sodate, CustomerName = so. CustomerName, Remark = so. Remark});Con. Execute (strSQL, so);

********************************************************************

4, Execute a command multiple times the same signature also allows  you to conveniently and efficiently execute a command multiple  times  (for example to bulk-load data) Example usage:

 connection. Execute (@ "   new  [] {new  {a=1 , B=1 }, new  {a=2 , B=2 }, new  {a=3 , B=3   3 ); //  3 rows inserted: "All", "2,2" and "3,3"  

This works for any parameter that implements IEnumerable for some T. This can be bulk inserted, good!

Notes on the use of dapper 1

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.