### 使用 VB.NET 开发完整的进销存管理系统代码示例
开发一个进销存管理系统需要涵盖多个功能模块,包括库存管理、销售管理、采购管理和报表生成等。以下是一个简化版的进销存管理系统代码示例,展示关键模块的核心实现逻辑。
#### 1. 数据库设计
进销存系统通常使用关系型数据库来存储数据。以下是一个简单的数据库表结构设计:
```sql
-- 商品表
CREATE TABLE Products (
ProductID INT PRIMARY KEY IDENTITY,
Name NVARCHAR(100),
Description NVARCHAR(MAX),
Price DECIMAL(18, 2),
Stock INT
);
-- 销售记录表
CREATE TABLE Sales (
SaleID INT PRIMARY KEY IDENTITY,
ProductID INT FOREIGN KEY REFERENCES Products(ProductID),
Quantity INT,
SaleDate DATETIME
);
-- 采购记录表
CREATE TABLE Purchases (
PurchaseID INT PRIMARY KEY IDENTITY,
ProductID INT FOREIGN KEY REFERENCES Products(ProductID),
Quantity INT,
PurchaseDate DATETIME
);
```
#### 2. 库存管理模块
库存管理模块允许用户查看和更新商品库存。
```vb
Imports System.Data.SqlClient
Public Class InventoryManagement
Private connectionString As String = "YourConnectionStringHere"
Public Sub AddProduct(ByVal name As String, ByVal description As String, ByVal price As Decimal, ByVal stock As Integer)
Using connection As New SqlConnection(connectionString)
Dim query As String = "INSERT INTO Products (Name, Description, Price, Stock) VALUES (@Name, @Description, @Price, @Stock)"
Using command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@Name", name)
command.Parameters.AddWithValue("@Description", description)
command.Parameters.AddWithValue("@Price", price)
command.Parameters.AddWithValue("@Stock", stock)
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
End Sub
Public Function GetProducts() As List(Of Product)
Dim products As New List(Of Product)()
Using connection As New SqlConnection(connectionString)
Dim query As String = "SELECT * FROM Products"
Using command As New SqlCommand(query, connection)
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
products.Add(New Product With {
.ProductID = Convert.ToInt32(reader("ProductID")),
.Name = reader("Name").ToString(),
.Description = reader("Description").ToString(),
.Price = Convert.ToDecimal(reader("Price")),
.Stock = Convert.ToInt32(reader("Stock"))
})
End While
End Using
End Using
End Using
Return products
End Function
End Class
Public Class Product
Public Property ProductID As Integer
Public Property Name As String
Public Property Description As String
Public Property Price As Decimal
Public Property Stock As Integer
End Class
```
#### 3. 销售管理模块
销售管理模块用于记录销售信息并更新库存。
```vb
Public Class SalesManagement
Private inventory As New InventoryManagement()
Public Sub RecordSale(ByVal productID As Integer, ByVal quantity As Integer)
Using connection As New SqlConnection(inventory.connectionString)
' 更新销售记录
Dim saleQuery As String = "INSERT INTO Sales (ProductID, Quantity, SaleDate) VALUES (@ProductID, @Quantity, GETDATE())"
Using saleCommand As New SqlCommand(saleQuery, connection)
saleCommand.Parameters.AddWithValue("@ProductID", productID)
saleCommand.Parameters.AddWithValue("@Quantity", quantity)
connection.Open()
saleCommand.ExecuteNonQuery()
End Using
' 更新库存
Dim updateStockQuery As String = "UPDATE Products SET Stock = Stock - @Quantity WHERE ProductID = @ProductID AND Stock >= @Quantity"
Using stockCommand As New SqlCommand(updateStockQuery, connection)
stockCommand.Parameters.AddWithValue("@ProductID", productID)
stockCommand.Parameters.AddWithValue("@Quantity", quantity)
If stockCommand.ExecuteNonQuery() = 0 Then
Throw New Exception("库存不足")
End If
End Using
End Using
End Sub
End Class
```
#### 4. 采购管理模块
采购管理模块用于记录采购信息并更新库存。
```vb
Public Class PurchasesManagement
Private inventory As New InventoryManagement()
Public Sub RecordPurchase(ByVal productID As Integer, ByVal quantity As Integer)
Using connection As New SqlConnection(inventory.connectionString)
' 更新采购记录
Dim purchaseQuery As String = "INSERT INTO Purchases (ProductID, Quantity, PurchaseDate) VALUES (@ProductID, @Quantity, GETDATE())"
Using purchaseCommand As New SqlCommand(purchaseQuery, connection)
purchaseCommand.Parameters.AddWithValue("@ProductID", productID)
purchaseCommand.Parameters.AddWithValue("@Quantity", quantity)
connection.Open()
purchaseCommand.ExecuteNonQuery()
End Using
' 更新库存
Dim updateStockQuery As String = "UPDATE Products SET Stock = Stock + @Quantity WHERE ProductID = @ProductID"
Using stockCommand As New SqlCommand(updateStockQuery, connection)
stockCommand.Parameters.AddWithValue("@ProductID", productID)
stockCommand.Parameters.AddWithValue("@Quantity", quantity)
stockCommand.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
```
#### 5. 报表生成模块
报表生成模块可以生成销售和采购的统计信息。
```vb
Public Class ReportGeneration
Private connectionString As String = "YourConnectionStringHere"
Public Function GenerateSalesReport(ByVal startDate As Date, ByVal endDate As Date) As List(Of Sale)
Dim sales As New List(Of Sale)()
Using connection As New SqlConnection(connectionString)
Dim query As String = "SELECT * FROM Sales WHERE SaleDate BETWEEN @StartDate AND @EndDate"
Using command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@StartDate", startDate)
command.Parameters.AddWithValue("@EndDate", endDate)
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
sales.Add(New Sale With {
.SaleID = Convert.ToInt32(reader("SaleID")),
.ProductID = Convert.ToInt32(reader("ProductID")),
.Quantity = Convert.ToInt32(reader("Quantity")),
.SaleDate = Convert.ToDateTime(reader("SaleDate"))
})
End While
End Using
End Using
End Using
Return sales
End Function
End Class
Public Class Sale
Public Property SaleID As Integer
Public Property ProductID As Integer
Public Property Quantity As Integer
Public Property SaleDate As Date
End Class
```
###