DAL层联合查询及条件查询方法(常用)

这篇博客介绍了在DAL层进行联合查询和条件查询的方法。首先,建议预先编写好SQL语句并确保其可执行。然后,通过SqlHelper将SQL语句应用到条件查询中。文中以实例说明,适合配合BLL层在UI层使用。
摘要由CSDN通过智能技术生成

DAL层联合查询及条件查询方法(常用)

首先我们要把SqlHelper给写完整,在我们往期博客内有写,可以点击主页查看哦~

1.我们首先要把Sql语句提前写好,在确认能运行的情况下再来写入查询方法内~

select ProInfo.*,ProType.TypeName from ProInfo,ProType where ProInfo.Protype=ProType.TypeID

我们用平时的案例来写这个查询,相信大家都可以看明白。

2.将sql语句带入进行条件查询

 p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的三架构DAL代码示例: 1. 数据访问接口 ```csharp public interface IProductRepository { IEnumerable<Product> GetAllProducts(); Product GetProductById(int id); void AddProduct(Product product); void UpdateProduct(Product product); void DeleteProduct(int id); } ``` 2. 数据访问实现 ```csharp public class ProductRepository : IProductRepository { private readonly IDbConnection _connection; public ProductRepository(IDbConnection connection) { _connection = connection; } public IEnumerable<Product> GetAllProducts() { return _connection.Query<Product>("SELECT * FROM Products"); } public Product GetProductById(int id) { return _connection.QuerySingle<Product>("SELECT * FROM Products WHERE Id = @Id", new { Id = id }); } public void AddProduct(Product product) { _connection.Execute("INSERT INTO Products (Name, Price) VALUES (@Name, @Price)", product); } public void UpdateProduct(Product product) { _connection.Execute("UPDATE Products SET Name = @Name, Price = @Price WHERE Id = @Id", product); } public void DeleteProduct(int id) { _connection.Execute("DELETE FROM Products WHERE Id = @Id", new { Id = id }); } } ``` 3. 依赖注入配置 ```csharp services.AddTransient<IDbConnection>(sp => new SqlConnection(Configuration.GetConnectionString("Default"))); services.AddTransient<IProductRepository, ProductRepository>(); ``` 上述代码中,数据访问接口定义了对产品实体进行增删改查的方法;数据访问实现通过连接到数据库并执行SQL语句来实现接口的方法;依赖注入配置将数据访问接口与具体实现关联起来,使其可以在应用程序中被使用。注意,这里使用了Dapper库来简化数据访问的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值