一、系统架构设计
1. 技术选型
- 开发语言:C# 10+(支持.NET 6+)
- 数据库:SQL Server 2019 Express(免费版)
- UI框架:Windows Forms(适合快速开发)
- 架构模式:分层架构(UI层+业务逻辑层+数据访问层)
2. 功能模块划分
pie
title 系统功能占比
"商品管理" : 30
"库存管理" : 25
"销售管理" : 25
"报表统计" : 15
"系统设置" : 5
二、核心功能实现
1. 数据库设计(SQL脚本)
-- 商品表
CREATE TABLE Products (
ProductID INT PRIMARY KEY IDENTITY(1,1),
Name NVARCHAR(100) NOT NULL,
Category NVARCHAR(50),
Price DECIMAL(10,2) CHECK (Price > 0),
Stock INT CHECK (Stock >= 0),
Barcode NVARCHAR(50) UNIQUE
);
-- 销售记录表
CREATE TABLE SalesRecords (
RecordID INT PRIMARY KEY IDENTITY(1,1),
ProductID INT FOREIGN KEY REFERENCES Products(ProductID),
SaleDate DATETIME DEFAULT GETDATE(),
Quantity INT CHECK (Quantity > 0),
TotalPrice DECIMAL(10,2)
);
2. 商品管理模块
// 商品实体类
public class Product {
public int ProductID { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public int Stock { get; set; }
public string Barcode { get; set; }
}
// 商品管理类(DAL层)
public class ProductDAL {
private string connectionString = "Data Source=.;Initial Catalog=SupermarketDB;Integrated Security=True";
// 添加商品
public void AddProduct(Product product) {
using (SqlConnection conn = new SqlConnection(connectionString)) {
string sql = @"INSERT INTO Products (Name,Category,Price,Stock,Barcode)
VALUES (@Name,@Category,@Price,@Stock,@Barcode)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Name", product.Name);
cmd.Parameters.AddWithValue("@Category", product.Category);
cmd.Parameters.AddWithValue("@Price", product.Price);
cmd.Parameters.AddWithValue("@Stock", product.Stock);
cmd.Parameters.AddWithValue("@Barcode", product.Barcode);
conn.Open();
cmd.ExecuteNonQuery();
}
}
}
3. 库存管理功能
// 库存更新逻辑
public void UpdateStock(int productId, int quantity) {
using (SqlConnection conn = new SqlConnection(connectionString)) {
string sql = "UPDATE Products SET Stock = Stock + @Quantity WHERE ProductID = @ID";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@ID", productId);
cmd.Parameters.AddWithValue("@Quantity", quantity);
conn.Open();
cmd.ExecuteNonQuery();
}
}
// 库存预警(定时任务)
public void CheckStockAlert() {
var lowStockItems = GetProductsByCondition(p => p.Stock < 10);
foreach (var item in lowStockItems) {
MessageBox.Show($"商品 {item.Name} 库存不足,当前库存:{item.Stock}");
}
}
4. 销售管理模块
// 销售记录类
public class SaleRecord {
public DateTime SaleTime { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public decimal TotalPrice { get; set; }
}
// 收银逻辑
public decimal ProcessSale(string barcode, int quantity) {
var product = GetProductByBarcode(barcode);
if (product == null || product.Stock < quantity) {
throw new Exception("商品不存在或库存不足");
}
decimal total = product.Price * quantity;
UpdateStock(product.ProductID, -quantity);
// 记录销售
using (SqlConnection conn = new SqlConnection(connectionString)) {
string sql = @"INSERT INTO SalesRecords (ProductID,Quantity,TotalPrice)
VALUES (@ProductID,@Quantity,@TotalPrice)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@ProductID", product.ProductID);
cmd.Parameters.AddWithValue("@Quantity", quantity);
cmd.Parameters.AddWithValue("@TotalPrice", total);
conn.Open();
cmd.ExecuteNonQuery();
}
return total;
}
三、界面设计示例(Windows Forms)
1. 商品管理界面
+---------------------------------+
| 商品列表 |
|--------------------------------|
| 编号 | 名称 | 价格 | 库存 |
|--------------------------------|
| 1 | 苹果 | 5.50 | 100 |
| 2 | 牛奶 | 12.0 | 50 |
+--------------------------------+
[添加] [编辑] [删除] [刷新]
2. 销售界面
+---------------------------------+
| 条码扫描区 |
|--------------------------------|
| 商品名称: 苹果 |
| 单价: 5.50 |
| 数量: [____] |
| 总价: 5.50 * [____] = [____] |
[确认销售] [取消]
+---------------------------------
四、关键技术实现
1. 数据访问层(DAL)
public class DatabaseHelper {
private static string connectionString = "Data Source=.;Initial Catalog=SupermarketDB;Integrated Security=True";
public DataTable GetData(string sql) {
using (SqlConnection conn = new SqlConnection(connectionString)) {
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
}
}
2. 业务逻辑层(BLL)
public class ProductService {
private ProductDAL productDAL = new ProductDAL();
public List<Product> GetAllProducts() {
string sql = "SELECT * FROM Products";
DataTable dt = new DatabaseHelper().GetData(sql);
return ConvertToProductList(dt);
}
}
五、扩展功能建议
1. 条码扫描支持
// 条码解析示例
public string ParseBarcode(string input) {
if (input.StartsWith("978")) { // ISBN-13
return input.Substring(3, 7);
}
return input;
}
2. 报表生成
// 销售报表
public void GenerateSalesReport(DateTime startDate, DateTime endDate) {
string sql = $@"
SELECT ProductName, SUM(Quantity) AS TotalSold,
SUM(TotalPrice) AS TotalRevenue
FROM SalesRecords
WHERE SaleDate BETWEEN @Start AND @End
GROUP BY ProductName";
DataTable dt = new DatabaseHelper().GetData(sql);
// 使用Crystal Reports生成PDF
}
六、部署与运行
1. 环境要求
- .NET Framework 4.8
- SQL Server 2019 Express
- Windows 10及以上系统
2. 部署步骤
- 安装SQL Server Express并创建数据库
- 运行数据库初始化脚本
- 编译发布C#项目
- 配置应用程序连接字符串
七、学习资源推荐
- 官方文档
- C# 控制台应用开发指南 docs.microsoft.com/zh-cn/dotnet/csharp/tour-of-csharp/
- SQL Server 基础教程 www.w3schools.com/sql/
- 实战案例
- 项目:C# 超市商品管理系统入门级源码(含数据库) youwenfan.com/contentcsh/93581.html
通过本方案,开发者可以快速掌握C#桌面应用开发的核心技能,包括数据库操作、界面设计和业务逻辑实现。建议从基础功能开始逐步扩展,结合实际需求添加会员管理、促销活动等高级功能。

被折叠的 条评论
为什么被折叠?



