dapper.simplecurd
[Table("Users")]//真实表名
publicclass User
{
[Key]
publicint UserId { get; set; }
[Column("strFirstName"] //真实列名
publicstring FirstName { get; set; }//列别名
publicstring LastName { get; set; }
publicint Age { get; set; }
} var user = connection.Get<User>();
改动后该查询相当于sql
Select UserId, strFirstName as FirstName, LastName, Age from[Users]where UserId =@UserID
2.GetList方法
publicstatic IEnumerable<T> GetList<T>(this IDbConnection connection)
publicclass User
{
publicint Id { get; set; }
publicstring Name { get; set; }
publicint Age { get; set; }
}
查询全部
var user = connection.GetList<User>();
相当于Sql
Select*from[User]
使用条件实体查询
publicclass User
{
publicint Id { get; set; }
publicstring Name { get; set; }
publicint Age { get; set; }
} var user = connection.GetList<User>(new { Age = 10 });
Select*from[User]where Age =@Age
//使用字符串条件查询
publicclass User
{
publicint Id { get; set; }
publicstring Name { get; set; }
publicint Age { get; set; }
} var user = connection.GetList<User>("where age = 10 or Name like '%Smith%'");
//相当于SQL
Select*from[User]where age =10or Name like'%Smith%'
//分页查询:
publicstatic IEnumerable<T> GetListPaged<T>(this IDbConnection connection, int pageNumber, int rowsPerPage, string conditions, stringorderby) publicclass User
{
publicint Id { get; set; }
publicstring Name { get; set; }
publicint Age { get; set; }
} var user = connection.GetListPaged<User>(1,10,"where age = 10 or Name like '%Smith%'","Name desc");
//相当于SQl:
SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY Name desc) AS PagedNumber, Id, Name, Age FROM [User] where age = 10 or Name like '%Smith%') AS u WHERE PagedNUMBER BETWEEN ((1 - 1) * 10 + 1) AND (1 * 10)
//插入方法
publicstaticint Insert(this IDbConnection connection, object entityToInsert) [Table("Users")]
publicclass User
{
[Key]
publicint UserId { get; set; }
publicstring FirstName { get; set; }
publicstring LastName { get; set; }
publicint Age { get; set; } //Additional properties not in database [Editable(false)]
publicstring FullName { get { returnstring.Format("{0} {1}", FirstName, LastName); } }
public List<User> Friends { get; set; }
[ReadOnly(true)]
public DateTime CreatedDate { get; set; }
} var newId = connection.Insert(new User { FirstName = "User", LastName = "Person", Age = 10 });
//相当于SQL
Insertinto[Users] (FirstName, LastName, Age) VALUES (@FirstName, @LastName, @Age)
//更新方法
[Table("Users")]public class User{
[Key]publicint UserId { get; set; }
[Column("strFirstName")]public string FirstName { get; set; }
public string LastName { get; set; }
publicint Age { get; set; } //Additional properties notindatabase[Editable(false)]public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
public List<User> Friends { get; set; }
}
connection.Update(entity);
//相当于SQL
Update[Users]Set (strFirstName=@FirstName, LastName=@LastName, Age=@Age) Where ID =@ID
删除方法:
public static intDelete<T>(this IDbConnection connection, int Id) public class User{
publicint Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
publicint Age { get; set; }
}
connection.Delete<User>(newid); 或 public static intDelete<T>(this IDbConnection connection, T entityToDelete) public class User{
publicint Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
publicint Age { get; set; }
} connection.Delete(entity);
相当于SQl
DeleteFrom[User]Where ID =@ID
删除多条
1.根据实体删除
public static int DeleteList<T>(this IDbConnection connection, object whereConditions, IDbTransaction transaction=null, int? commandTimeout =null) connection.DeleteList<User>(new { Age =10 }); 2.根据条件删除
public static int RecordCount<T>(this IDbConnection connection, string conditions = "") connection.DeleteList<User>("Where age >20");
统计
public static int RecordCount<T>(this IDbConnection connection, string conditions = "") varcount= connection.RecordCount<User>("Where age >20");
dapper.simplecurd的更多相关文章
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- Dapper扩展之~~~Dapper.Contrib
平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...
- 由Dapper QueryMultiple 返回数据的问题得出==》Dapper QueryMultiple并不会帮我们识别多个返回值的顺序
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 今天帮群友整理Dapper基础教程的时候手脚快了点,然后遇到了一个小问题,Dapp ...
- Dapper.Contrib:GetAsync<T> only supports an entity with a [Key] or an [ExplicitKey] property
异常处理:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 原来Model是这样滴 修改后是这样滴 注意点:Model里面的Table和Key ...
- Dapper where Id in的解决方案
简单记一下,一会出去有点事情~ 我们一般写sql都是==>update NoteInfo set NDataStatus=@NDataStatus where NId in (@NIds) Da ...
- ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)
操作 MySql 数据库使用MySql.Data程序包(MySql 开发,其他第三方可能会有些问题). project.json 代码: { "version": "1. ...
- Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记
0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...
- 搭建一套自己实用的.net架构(3)续 【ORM Dapper+DapperExtensions+Lambda】
前言 继之前发的帖子[ORM-Dapper+DapperExtensions],对Dapper的扩展代码也进行了改进,同时加入Dapper 对Lambda表达式的支持. 由于之前缺乏对Lambda的知 ...
- mono for android中使用dapper或petapoco对sqlite进行数据操作
在mono for android中使用dapper或petapoco,很简单,新建android 类库项目,直接把原来的文件复制过来,对Connection连接报错部分进行注释和修改就可以运行了.( ...
随机推荐
- Ubuntu 18.04.1安装Nginx
一.安装Nginx所需的环境 Ubuntu可以通过apt源安装以下依赖库,CentOS可以通过yum安装 1.Nginx是C语言开发,需要gcc依赖库 先检查本机是否有gcc环境 gcc -v 如果没 ...
- Windows7下Jupyter Notebook使用入门
目录 一.Jupyter简介 二.Jupyter安装 2.1 python 3安装 2.2 Jupyter 安装 三.Jupyter使用示例 四.Jupyter常用命令 五.其他说明 一.Jupyte ...
- Android 正则表达式实例
editText正则表达式的使用 检查输入是否符合规则 import Android.app.Activity; import android.os.Bundle; import android.vi ...
- webpack打包vue项目,资源路径如何从绝对路径改为相对路径?css中的图片资源如何修改配置?
资源相对引用路径 问题描述 一般情况下,通过webpack+vuecli默认打包的css.js等资源,路径都是绝对的. 但当部署到带有文件夹的项目中,这种绝对路径就会出现问题,因为把配置的static ...
- SQL Server 创建索引方法
转自 <SQL Server 创建索引的 5 种方法> 地址:https://www.cnblogs.com/JiangLe/p/4007091.html 前期准备: create tab ...
- Linux中安装tomcat后,window中访问不到tomcat的欢迎界面问题
首先,可以通过xftp把下载的tomcat的tar.gz包传输到Linux中. 然后进行解压,tar -zxvf tomcat的压缩包名称(可以使用tab键快速补齐) 解压后,可以使用修改/con ...
- 使用有序GUID:提升其在各数据库中作为主键时的性能
原文出处:https://www.codeproject.com/articles/388157/guids-as-fast-primary-keys-under-multiple-database ...
- linux—epoll
一.epoll服务端实现中需要的3个函数: epoll_create:创建保存epoll文件描述符的空间. epoll_ctl:向空间注册并注销文件描述符. epoll_wait:与select函数类 ...
- bind与继承 待研究
class a { f() { console.log('a') } get f2() { console.log('f2') return (this['f'] = this.f.bind(this ...
- mpvue学习笔记(一) 基础介绍
一. 小程序环境搭建 后台地址:https://mp.weixin.qq.com/ 文档地址:https://developers.weixin.qq.com/miniprogram/dev/ 注册账 ...