zoukankan      html  css  js  c++  java
  • 关于Dapper的使用笔记2

    1 在实体定义中标示字段名:

    public class t_SoDtl
    {
        [System.ComponentModel.Description("KeySeq")]
        public Guid ID { get; set; } //数据库中的列名KeySeq
    
        [System.ComponentModel.Description("SoKeySeq")]
        public Guid SoKeySeq { get; set; }
    
        [System.ComponentModel.Description("ItemId")]
        public string ItemNo { get; set; }
    
        [System.ComponentModel.Description("ItemName")]
        public string ItemName { get; set; }
    
        [System.ComponentModel.Description("Price")]
        public decimal Price { get; set; }
    
        [System.ComponentModel.Description("Qty")]
        public decimal Quality { get; set; }//数据库中的列名Qty
    
        [System.ComponentModel.Description("Amount")]
        public decimal Amount { get; set; }
    }

    2、建立自定义映射 SqlMapper.ITypeMap ---> CustomPropertyTypeMap

    var map = new CustomPropertyTypeMap(typeof(t_SoDtl),
    (type, columnName) => type.GetProperties()
        .Where(prop => prop.GetCustomAttributes(false).OfType<DescriptionAttribute>().Any(attr => attr.Description == columnName)) //找出Decsription = Column的属性
        .FirstOrDefault());
    
    Dapper.SqlMapper.SetTypeMap(typeof(t_SoDtl), map);

    3、效果测试

    //查询
    DbConnection con = this.GetConnection();
    return con.Query<t_SoDtl>("select * from dbo.t_SoDtl where SoKeySeq = @SoKeySeq", new {SoKeySeq = soKeySeq}).ToList();
    
    //插入 (注意参数的名称与实体属性的对应)
    string strSQL = @"
    INSERT INTO dbo.t_so(KeySeq,SoNo,SoDate,CustomerName,Remark)
    VALUES(@KeySeq,@SoNo,@SoDate,@CustomerName,@Remark)";
    
    DbConnection con = this.GetConnection();
    //con.Execute(strSQL, new {KeySeq = so.KeySeq, SoNo = so.SoNo, SoDate = so.SoDate, CustomerName = so.CustomerName, Remark = so.Remark});
    con.Execute(strSQL, so);
  • 相关阅读:
    工作的本质是思考
    V8、JSCore、Hermes、QuickJS,hybrid开发JS引擎怎么选
    Aspects框架的源码解读及问题解析
    饿了么移动APP的架构演进
    关键字:客户端架构演进
    以小见大,见微知著——亿万级APP架构演进之路
    理解 Swift:ObjectiveC 的构建管道
    MMKV 组件现在开源了
    进阶:iOS 性能优化系列
    你如果无法度量它,就无法管理它
  • 原文地址:https://www.cnblogs.com/shi5588/p/4126873.html
Copyright © 2011-2022 走看看