SQL引用DAL

步骤:https://www.cnblogs.com/grom/articles/8981116.html

异常:

解决方案:

执行

ALTER DATABASE [DBName] SET TRUSTWORTHY ON

 

转载于:https://www.cnblogs.com/grom/p/9639221.html

  • 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、付费专栏及课程。

余额充值