关于Dapper的使用笔记2

 

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; }
}

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);

//查询
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);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。