一、三层架构分为:BLL层:业务逻辑层,DAL层:数据访问层,UI层:界面 Model不是一层
UI层是调用BLL层的,BLL调用DAL层,每层都必须调用Model层
Model层:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Model { public class ProductInfo { public string PId { get; set; } public string PName { get; set; } public decimal Price { get; set; } public int Amount { get; set; } } }
DAL层如下:用来存放方法层
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Model; using System.Data.SqlClient; namespace DAL { public class ProductInfoService { //查询所有商品信息 public static List<ProductInfo> SelectProductsAll() { List<ProductInfo> pros = new List<ProductInfo>(); string sql = "select * from ProductInfo"; SqlDataReader sdr = DBHelper.ExecuteReader(sql); while (sdr.Read()) { ProductInfo pro = new ProductInfo(); pro.PId = sdr["PId"] + ""; pro.PName = sdr["PName"] + ""; pro.Price = Convert.ToDecimal(sdr["Price"]); pro.Amount = (int)sdr["Amount"]; pros.Add(pro); } sdr.Close(); DBHelper.CloseCon(); return pros; } //根据商品ID查询商品信息 public static List<ProductInfo> SelectProductsByPId(string pId) { List<ProductInfo> pros = new List<ProductInfo>(); string sql = "select * from ProductInfo where PId like '%" + pId+"%'"; SqlDataReader sdr = DBHelper.ExecuteReader(sql); while (sdr.Read()) { ProductInfo pro = new ProductInfo(); pro.PId = sdr["PId"] + ""; pro.PName = sdr["PName"] + ""; pro.Price = Convert.ToDecimal(sdr["Price"]); pro.Amount = (int)sdr["Amount"]; pros.Add(pro); } sdr.Close(); DBHelper.CloseCon(); return pros; } //添加商品 public static int InsertProduct(ProductInfo pi) { int n = 0; string sql = string.Format("insert ProductInfo values('{0}','{1}','{2}','{3}')", pi.PId, pi.PName, pi.Price, pi.Amount); n = DBHelper.ExecuteNonQuery(sql); return n; } //修改商品 public static int UpdateProduct(ProductInfo pi) { int n = 0; string sql = string.Format("update ProductInfo set PName='{0}',Price='{1}',Amount='{2}' where PId='{3}'", pi.PName, pi.Price, pi.Amount, pi.PId); n = DBHelper.ExecuteNonQuery(sql); return n; } //删除商品 public static int DeleteProduct(string pid) { int n = 0; string sql = "Delete from ProductInfo where PId='" + pid+"'"; n = DBHelper.ExecuteNonQuery(sql); return n; } } }
BLL层如下:相当于桥梁
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DAL; using Model; using System.Data.SqlClient; namespace BLL { public class ProductManager { //查询所有商品信息 public static List<ProductInfo> SelectProductsAll() { return ProductInfoService.SelectProductsAll(); } //根据商品ID查询商品信息 public static List<ProductInfo> SelectProductsByPId(string pId) { return ProductInfoService.SelectProductsByPId(pId); } //删除商品 public static int DeleteProduct(string pid) { return ProductInfoService.DeleteProduct(pid); } public static int InsertProduct(ProductInfo pi) { return ProductInfoService.InsertProduct(pi); } //修改商品 public static int UpdateProduct(ProductInfo pi) { return ProductInfoService.UpdateProduct(pi); } } }
UI层直接调用即可。