VS 三层架构DAL层Sqlhelper代码块

 先引用

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

 public class SqlHelper
    {
        /// <summary>
        /// 创建连接字符串
        /// </summary>
        private static string strConn = ConfigurationManager.ConnectionStrings["DigitalProductShopConnectionString"]
            .ConnectionString;

        /// <summary>
        /// 查询语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable Query(string sql)
        {
            //创建一张新表
            using (DataTable table = new DataTable())
            {
                //创建数据适配器
                using (SqlDataAdapter adapter = new SqlDataAdapter(sql, strConn))
                {
                    adapter.Fill(table);
                }

                return table;
            }
        }

        public static int NonQuery(string sql)
        {
            //创建一个数字
            int num = 0;
            using (SqlConnection sqlConn = new SqlConnection(strConn))
            {
                using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
                {
                    try
                    {
                        sqlConn.Open();
                        num = sqlCmd.ExecuteNonQuery();
                    }
                    finally
                    {
                        if (sqlConn.State == ConnectionState.Open)
                        {
                            sqlConn.Close();
                        }
                    }
                }
            }
            return num;
        }
    }

 

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

余额充值