标签:
一些关于Dapper的介绍:
1.Dapper是一个轻型的开源ORM类,代码就一个SqlMapper.cs文件,编译后就40多K的一个很小的Dll.
2.Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的数据库
3.Dapper的r支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性。
4.Dapper原理通过Emit反射IDataReader的序列队列,来快速的得到和产生对象。性能提升了很多;(比采用常规的反射)并且无须迁就数据库的设计。
Dapper源码下载链接:
点击这里下载dapper源代码。
Demo:
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源代码下载
标签:
原文地址:http://www.cnblogs.com/Francis-YZR/p/5528299.html