企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## **基本格式** ~~~ using (DapperDbContext Context = new DapperDbContext("Default")) {   } ~~~ Default 是数据库链接方式 ,在web 项目下面的 Web.config 中的 connectionStrings 节点 ![](https://img.kancloud.cn/eb/5d/eb5dc72ad39b4d709c900ac5055faa07_1681x675.png) ### 设置实体类配置 实体类 ![](https://img.kancloud.cn/2f/fa/2ffa7be08ff9c797c0d87d3e4477cac4_647x418.png) ## **KeyType 类型选用** 1、KeyType.Identity:CPS通过EF 创建的表都用这个 2、KeyType.TriggerIdentity:sql语句创建的表,并且做了自增的触发器,用这个 3、KeyType.Assigned:数据库表不自增,要用代码来实现自增效果的,用这个 4、KeyType.GUID:主键是GUID类型的,使用这个(这个没用过) 主键自增 ![](https://img.kancloud.cn/91/f0/91f0a6368c8cd3df717541050c83f079_878x286.png) ~~~ public class QCTemp1Mapper : ClassMapper<QCTemp1> {     public QCTemp1Mapper()     {         Table("QC_TEMP_1");         Map(p => p.Id).Key(KeyType.TriggerIdentity);         // ID主键自增, 并且为数值型的时候 使用 KeyType.Identity         AutoMap();     } } ~~~ 主键不自增配置方式 ![](https://img.kancloud.cn/fb/c4/fbc48337af5bdd5b94cd702e0fc98d19_536x239.png) ~~~ public class QCTemp2Mapper : ClassMapper<QCTemp2>     {         public QCTemp2Mapper()         {             Table("QC_TEMP_2");             Map(p => p.Id).Key(KeyType.Assigned);             AutoMap();         }     } } ~~~ ## **普通查询** ~~~ using (DapperDbContext Context = new DapperDbContext("Default")) { string sql = "select max(to_number(id)) as id from QC_TEMP_2";             QCTemp2 temp = Context.Query<QCTemp2>(sql).FirstOrDefault(); } ~~~ ## **插入数据** ~~~ using (DapperDbContext Context = new DapperDbContext("Default")) {                     //查询 最大id号,不自增的表需要,自己做自增字段,所以需要先查出本表最大id号                     string sql = "select max(to_number(id)) as id from QC_TEMP_2";                     QCTemp2 temp = Context.Query<QCTemp2>(sql).FirstOrDefault();                     input.Id = temp.Id + 1;                     Context.Insert<QCTemp2>(input); } ~~~ ## **修改** ~~~ using (DapperDbContext Context = new DapperDbContext("Default")) {                 Context.Update<QCTemp2>(dto); } ~~~ ## **删除** ~~~ using (DapperDbContext Context = new DapperDbContext("Default")) {                QCTemp2 temp = new QCTemp2();                temp.Id = input.Id;                Context.Delete<QCTemp2>(temp); } ~~~ ## **分页查询** ~~~ public async Task<PagedResultDto<QCTemp1>> GetQCTemp1Page(GetProcessListInput input) {            int nCount = 0;            string where = "1=1";            List<QCTemp1> nResults = new List<QCTemp1>();            if (!string.IsNullOrEmpty(input.AddCode))            {                where = where + $" and AddCode like '{input.AddCode}%'";            }            using (DapperDbContext Context = new DapperDbContext("Default"))            {                StringBuilder sb = new StringBuilder();                sb.AppendFormat("SELECT COUNT(1) as id FROM QC_TEMP_1 where {0}", where);                QCTemp2 temp = Context.Query<QCTemp2>(sb.ToString()).FirstOrDefault();                nCount = (int)temp.Id;                int pagesize = 20;                if (input.MaxResultCount != null && input.MaxResultCount > 0)                {                    pagesize = input.MaxResultCount;                }                nResults = Context.GetPageList2<QCTemp1>("QC_TEMP_1", where, "id", input.SkipCount, pagesize).ToList();            }            return new PagedResultDto<QCTemp1>(nCount, nResults); } ~~~ ~~~ public IEnumerable<TEntity> GetPageList2<TEntity>(string tableName, string where, string orderby, int skip, int pageSize) where TEntity : class {             StringBuilder sb = new StringBuilder();             sb.AppendFormat(@"SELECT  *                                 FROM(SELECT ROW_NUMBER() OVER(ORDER BY {2}) AS num,{0}.*                                           FROM  {0}                                           WHERE {1}                                         )  result                                 WHERE  num >= {3} AND num <= {4} ORDER BY {2} desc", tableName, where, orderby, skip, skip + pageSize);             return Query<TEntity>(sb.ToString()); } ~~~