Dapper是一款轻量级ORM工具(Github)。如果你在小的项目中,使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀。你又觉得ORM省时省力,这时Dapper 将是你不二的选择。
为什么选择Dapper
- 轻量。只有一个文件(SqlMapper.cs),编译完成之后只有120k(好象是变胖了)
- 速度快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。
- 支持多种数据库。Dapper可以在所有Ado.net Providers下工作,包括sqlite, sqlce, firebird, oracle, MySQL, PostgreSQL and SQL Server
- 可以映射一对一,一对多,多对多等多种关系。
- 性能高。通过Emit反射IDataReader的序列队列,来快速的得到和产生对象,性能不错。
- 支持FrameWork2.0,3.0,3.5,4.0,4.5
首先从
//获取一个连接字符串 private static readonly string constr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; //写一个打开数据库操作 public static IDbConnection GetDbConnection() { IDbConnection connection = new SqlConnection(constr); if (connection.State == ConnectionState.Closed) { connection.Open(); } return connection; }
在数据访问层的展示
记得引用 using Dapper;
//简单查询
public static List<UserInfo> Show() { using (IDbConnection connection =DBHelper.GetDbConnection()) { return connection.Query<UserInfo>("查询语句").ToList(); } }
//简单添加 public static int Insert(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("insert into Person(Name,Remark) values(@Name,@Remark)", person); } }
//删除操作 public static int Delete(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("delete from Person where id=@ID", person); } } public static int Delete(List<Person> persons) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("delete from Person where id=@ID", persons); } }
//修改操作 public static int Update(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("update Person set name=@name where id=@ID", person); } } public static int Update(List<Person> persons) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("update Person set name=@name where id=@ID", persons); } }
/// <summary> /// 无参查询所有数据 /// </summary> /// <returns></returns> public static List<Person> Query() { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Query<Person>("select * from Person").ToList(); } } /// <summary> /// 查询指定数据 /// </summary> /// <param name="person"></param> /// <returns></returns> public static Person Query(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Query<Person>("select * from Person where id=@ID",person).SingleOrDefault(); } }
//查询的In操作 /// <summary> /// In操作 /// </summary> public static List<Person> QueryIn() { using (IDbConnection connection = new SqlConnection(connectionString)) { var sql = "select * from Person where id in @ids"; //参数类型是Array的时候,dappper会自动将其转化 return connection.Query<Person>(sql, new { ids = new int[2] { 1, 2 }, }).ToList(); } } public static List<Person> QueryIn(int[] ids) { using (IDbConnection connection = new SqlConnection(connectionString)) { var sql = "select * from Person where id in @ids"; //参数类型是Array的时候,dappper会自动将其转化 return connection.Query<Person>(sql, new { ids }).ToList(); } }
控制器展示
//查询数据
public IEnumerable<UserInfo> Show(int iPageIndex = 1, int iPageSize = 2) { List<UserInfo> list = new List<UserInfo>(); list = dal.Show();
//由此实现一个分页操作 list= list.Skip((PageSize-1) * PageIndex).Take(PageSize).ToList();return list; }
public int Del(int id) { return dal.Delete(id); }
下面是另一种写法
1 //数据库连接 2 string str = ""; 3 4 //添加 5 [HttpPost] 6 public int Add(ModelInfo mf) 7 { 8 using (SqlConnection conn = new SqlConnection(str)) 9 { 10 string sql = $"insert into Goods values ('{mf.Name}','{mf.Pwd}')"; 11 return conn.Execute(sql, mf); 12 } 13 } 14 15 //显示 16 [HttpGet] 17 public List<ModelInfo> Show() 18 { 19 using (SqlConnection conn = new SqlConnection(str)) 20 { 21 ModelInfo mf = new ModelInfo(); 22 string sql = $"select * from Goods"; 23 return conn.Query<ModelInfo>(sql, mf).ToList(); 24 } 25 } 26 //查询 27 [HttpGet] 28 public List<ModelInfo> Select(string name) 29 { 30 using (SqlConnection conn = new SqlConnection(str)) 31 { 32 string sql = $"select * from Goods where Name like '%{name}%'"; 33 return conn.Query<ModelInfo>(sql, name).ToList(); 34 } 35 } 36 37 //删除 38 //vs的BUG 删除不用Get出不来 39 [HttpGet] 40 public int Del(string id) 41 { 42 using (SqlConnection conn = new SqlConnection(str)) 43 { 44 string sql = $"delete Goods where ID = {id}"; 45 return conn.Execute(sql, id); 46 } 47 } 48 49 //修改 50 [HttpPut] 51 public int Upd(ModelInfo mf) 52 { 53 using (SqlConnection conn = new SqlConnection(str)) 54 { 55 string sql = $"update Goods set Pwd = '{mf.Pwd}' where ID = {mf.ID} "; 56 return conn.Execute(sql, mf); 57 } 58 }