[私人小抄] Dapper

Dapper 小抄

常用方式

//新增
var Id1 = cnn.Query<long>(sql, customer).First();
var Id2 = cnn.QueryFirst<long>(sql, customer);
var Id3 = cnn.QueryFirstOrDefault<long>(sql, customer);
var Id4 = cnn.QuerySingle<long>(sql, customer);
var Id5 = cnn.QuerySingleOrDefault<long>(sql, customer);

//刪除 更新
var row = cnn.Execute(sql,new {Data="ABC" });

//多個查詢
var p = new DynamicParameters();
p.Add("@Id", 1);
var gridReader = cnn.QueryMultiple(sql,p);
var totalCount1 = gridReader.ReadSingle<int>();
var result1 = gridReader.ReadSingleOrDefault<Customer>();
gridReader.Dispose();

//查不到東西會是空集合 不是null
var list = cnn.Query<Customer>(sql);
var notNull = list.Count() == 0;

 

如果內容有誤請多鞭策謝謝