Dapper的语法应用

本文介绍了通过连接数据库执行不同类型的操作,包括获取供应商代码、添加包装、查询物料信息、获取供应商列表及进行增删改查等常见业务场景的具体实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)返回某个整型或字符串类型的字段

public string GetSupplierCodeById(int Id)
{ 
using( var conn=DbFactory.CreateConnection())
{
var result = conn.Query<dynamic>(GetSupplierCodeByIdSql, new { Id = Id }).FirstOrDefault();
return result.Code;
}
}
/// 添加包装
        /// </summary>
        /// <param name="p"></param>
        public int AddPackSP(Pack p)
        {
            using (var conn = DbFactory.CreateConnection())
            {
                //var result = conn.Execute(AddPackSPSql, p);
                var result = conn.Query<dynamic>(AddPackSPSql, p).FirstOrDefault();
                return result != null ? Convert.ToInt32(result.PackId) : 0;
            }
        }

 

(2)返回一个对象

 public Material GetMaterialByCode(string code)
{
      using (DbConnection conn = DbFactory.CreateConnection())
      {
          var result = conn.Query<Material>(GetMaterialByCodeSql,
          new { Code = code }).FirstOrDefault();
           return result;
       }
 }

(3)返回一个集合

   返回一条数据:

   public IList<Supplier> GetValidSupplier(int employeeId)
        {
            using (var conn = DbFactory.CreateConnection())
            {
                var result = conn.Query<Supplier>(GetValidSupplierByEIdSql,
                  new { EmployeeId = employeeId }).ToList();
                return result;
            }
        }

返回多条数据:

 public IList<Supplier> GetSupplierList(int supplierType)
        {
            using (var conn = DbFactory.CreateConnection())
            {
                var result = conn.QueryMultiple(GetSupplierListBySupplierTypeSql,
                    new { SupplierType = supplierType }).Read<Supplier>().ToList();
                return result;
            }
        }

(4)返回bool类型(判断是否增删改成功或是否被引用等)

 public bool IsUsed(int supplierId)
        {
            using (var conn = DbFactory.CreateConnection())
            {
                var result = conn.QueryMultiple(IsUsedSql,
                    new { SupplierId = supplierId }).Read<int>().Single() > 0;
                return result;
            }
        }
 public bool DeleteMaterial(int materialId)
        {
            using (DbConnection conn = DbFactory.CreateConnection())
            {
                bool result = conn.Execute(DeleteMaterialSql, new { Id = materialId }) > 0;

                return result;
            }
        }

转载于:https://www.cnblogs.com/abc8023/p/3843339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值