Some introductions about dapper:
1.Dapper is a lightweight open-source ORM class, the code is a SqlMapper.cs file, compiled over 40 K of a very small DLL.
2.Dapper supports a series of databases such as Mysql,sqllite,mssql2000,mssql2005,oracle
The 3.Dapper R supports multi-table parallel objects. Support a one-to-many relationship. And it's not invasive.
The 4.Dapper principle is to quickly get and produce objects by emit the sequence queue that reflects IDataReader. Performance has improved a lot, (compared with conventional reflection) and does not have to accommodate the database design.
Dapper Source Download link:
Click here to download the dapper source code.
Demo:
varConnection =getopenconnection (); varGUID =Guid.NewGuid (); stringID ="6e2a106d-d838-48b9-ac74-ad604457bba2"; //1 Generic type varDog = connection. Query<dog> ("SELECT * from Dog where Id = @Id",New{Id =ID}); //2 Dynamic parsing varrows = connection. Query ("SELECT * from Dog where Id = @Id",New{Id =ID}). ToList (); foreach(DynamicIteminchrows) {String Hacker=item. Name; }
Query
// 3. Execution does not return results int result=connection. Execute ("insert into Dog values (@age, @id, @name, @weight, @ignoredproperty)"new the " YZR " 117.5 1 });
Execute
//4. Batch Executelist<dog> list =NewList<dog>(); List. ADD (NewDog () {age =9, Id = Guid.NewGuid (), Name ="zxx", Weight = - }); List. ADD (NewDog () {age =9, Id = Guid.NewGuid (), Name ="WMJ", Weight = - }); List<Dynamic> paramslist =Newlist<Dynamic>(); foreach(Dog Iteminchlist) {Paramslist.add (New{age = Item. Age, id = Item. ID, name = Item. Name, Weight = Item. Weight, Ignoredproperty =1 }); } intresult = connection. Execute ("INSERT into Dog values (@age, @id, @name, @weight, @ignoredproperty)", paramslist);
Bulk Execute
//5.In OperationDog = connection. Query<dog> ("SELECT * from the Dog where ID in @ids",New{ids =NewString[] {"6e2a106d-d838-48b9-ac74-ad604457bba3","6e2a106d-d838-48b9-ac74-ad604457bba2","535dab3a-d3c1-4cb0-b8f7-63351f491056" } }); //equivalent toDog = connection. Query<dog> ("SELECT * from Dog where ID in (@id1, @id2, @id3)",New{ID1 ="6e2a106d-d838-48b9-ac74-ad604457bba3", Id2 ="6e2a106d-d838-48b9-ac74-ad604457bba2", ID3 ="535dab3a-d3c1-4cb0-b8f7-63351f491056"});
In operation
//database to establish a primary foreign key relationship varsql =@"Select p.*,u.* from the Dog p left join Owner u on u.ownerid = P.id where [email protected] Order by P.id"; Dog D=NULL; varT = connection. Query (SQL,New{id ="6e2a106d-d838-48b9-ac74-ad604457bba2" }); vardata = connection. Query<dog, Owner, dog> (SQL, (post, user) = = { if(d = =NULL|| D.id! =Post. ID) {D=Post; } if(User! =NULL) {d.user.add (user); } returnPost; }, New{id ="6e2a106d-d838-48b9-ac74-ad604457bba2"});
One -to-many relationships
//result set for multiple queriessql =@"SELECT * from the Dog where Id = @id select * from Owner"; using(varMulti = connection. Querymultiple (SQL,New{id ="6e2a106d-d838-48b9-ac74-ad604457bba3" })) { vardata = multi. Read<dog>(). ToList (); varOwner = multi. Read<owner>(). ToList (); }
Querymultiple
//Transactions using(connection) {//Start a transactionIDbTransaction transaction =connection. BeginTransaction (); Try { stringquery ="Update Dog set age=age+1 where [email protected]"; stringQuery2 ="Update Dog set weight=weight+1.0 where [email protected]"; Connection. Execute (Query,New{Id ="71fff309-c7c9-4f64-b588-0a03e27459ba"}, Transaction,NULL,NULL); Connection. Execute (Query2,New{Id ="71fff309-c7c9-4f64-b588-0a03e27459ba"}, Transaction,NULL,NULL); //Commit a transactionTransaction.commit (); } Catch(Exception ex) {//exception occurred, transaction rollbacktransaction. Rollback (); Throw NewException (ex. Message); } }
idbtransaction
//Stored Procedures varp =Newdynamicparameters (); //Example 1//p.add ("@result", DbType:DbType.Int32, Direction:ParameterDirection.Output); //var user = connection. Query ("Test", p, commandType:CommandType.StoredProcedure); //int totalcount = p.get<int> ("@result"); //Example 2//NOTE: Calling if you use query, the stored procedure requires a select result set.//p.add ("@result", DbType:DbType.Int32, Direction:ParameterDirection.Output); //p.add ("@rowcount", DbType:DbType.Int32, Direction:ParameterDirection.ReturnValue); //var user = connection. Query ("TestValue", p, commandType:CommandType.StoredProcedure); //int re = p.get<int> ("@result"); //int ro = p.get<int> ("@rowcount"); //Example 3//the stored procedure paging requires row_number () over (the order by ID) This sort of ID needs to be specifiedP.add ("@PageIndex",1, DbType:DbType.Int32, direction:ParameterDirection.Input); P.add ("@PageSize",3, DbType:DbType.Int32, direction:ParameterDirection.Input); P.add ("@TableName","Dog", dbType:DbType.String, direction:ParameterDirection.Input); P.add ("@Where","1=1 ORDER by ID ASC", dbType:DbType.String, direction:ParameterDirection.Input); //p.add ("@rowcount", DbType:DbType.Int32, direction:ParameterDirection.Output);P.add ("@rowcount", DbType:DbType.Int32, Direction:ParameterDirection.ReturnValue); varresult = connection. Query ("Selectbase", p, commandType:CommandType.StoredProcedure); intCount = p.get<int> ("@rowcount");
Procedure
// Switch Database Connection. ChangeDatabase (" database name ");
ChangeDatabase
Demo Source code Download
Click here to download
dapper-Open Source Small orm