一些關於Dapper的介紹:
java
1.Dapper是一個輕型的開源ORM類,代碼就一個SqlMapper.cs文件,編譯後就40多K的一個很小的Dll. sql
2.Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的數據庫數據庫
3.Dapper的r支持多表並聯的對象。支持一對多 多對多的關係。而且沒侵入性。app
4.Dapper原理經過Emit反射IDataReader的序列隊列,來快速的獲得和產生對象。性能提高了不少;(比採用常規的反射)而且無須遷就數據庫的設計。ide
Dapper源碼下載連接:post
Demo:ui
var connection = GetOpenConnection(); var guid = Guid.NewGuid(); string id = "6e2a106d-d838-48b9-ac74-ad604457bba2"; //1泛型 var dog = connection.Query<Dog>("select * from Dog where Id = @Id", new { Id = id }); //2 動態解析 var rows = connection.Query("select * from Dog where Id = @Id", new { Id = id }).ToList(); foreach (dynamic item in rows) { String 黑客 = item.Name; }
//3.執行不返回結果 int result=connection.Execute("insert into Dog values(@age,@id,@name,@weight,@ignoredproperty)", new { age = 49, id = Guid.NewGuid(), name = "YZR", weight = 117.5, ignoredproperty = 1 });
//4.批量Execute List<Dog> list = new List<Dog>(); list.Add(new Dog() { Age = 9, Id = Guid.NewGuid(), Name = "ZXX", Weight = 120 }); list.Add(new Dog() { Age = 9, Id = Guid.NewGuid(), Name = "WMJ", Weight = 120 }); List<dynamic> paramsList = new List<dynamic>(); foreach (Dog item in list) { paramsList.Add(new { age = item.Age, id = item.Id, name = item.Name, weight = item.Weight, ignoredproperty = 1 }); } int result = connection.Execute("insert into Dog values(@age,@id,@name,@weight,@ignoredproperty)", paramsList);
//5.In操做 dog = connection.Query<Dog>("select * from Dog where id in @ids", new { ids = new String[] { "6e2a106d-d838-48b9-ac74-ad604457bba3", "6e2a106d-d838-48b9-ac74-ad604457bba2", "535dab3a-d3c1-4cb0-b8f7-63351f491056" } }); //等價於 dog = 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" });
//數據庫要創建主外鍵關係 var sql = @"select p.*,u.* from Dog p left join Owner u on u.OwnerId = p.Id where p.id=@id Order by p.Id"; Dog d = null; var t = connection.Query(sql, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba2" }); var data = connection.Query<Dog, Owner, Dog>(sql, (post, user) => { if (d == null || d.Id != post.Id) { d = post; } if (user != null) { d.user.Add(user); } return post; }, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba2" });
//多查詢的結果集 sql = @"select * from Dog where Id = @id select * from Owner"; using (var multi = connection.QueryMultiple(sql, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba3" })) { var data = multi.Read<Dog>().ToList(); var owner = multi.Read<Owner>().ToList(); }
//事務 using (connection) { //開始事務 IDbTransaction transaction = connection.BeginTransaction(); try { string query = "update Dog set Age=Age+1 where Id=@Id"; string query2 = "update Dog set Weight=Weight+1.0 where Id=@Id"; 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); //提交事務 transaction.Commit(); } catch (Exception ex) { //出現異常,事務Rollback transaction.Rollback(); throw new Exception(ex.Message); } }
//存儲過程 var p = new DynamicParameters(); //實例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"); //實例2 //注意點:調用若是使用query,那麼存儲過程須要有select結果集 //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"); //實例3 //存儲過程分頁須要row_number() over( order by id)這個排序的id須要指定 p.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); var result = connection.Query("SelectBase", p, commandType: CommandType.StoredProcedure); int count = p.Get<int>("@rowcount");
//切換數據庫 connection.ChangeDatabase("數據庫名稱");
Demo源代碼下載spa
點擊這裏下載.net