11.19库存管理
using System; using System.Data.SqlClient; using System.Windows.Forms; using System.Collections.Generic; using System.Drawing; namespace SnacksInventorySystem { public partial class FormInventoryManagement : Form { private List<Goods> goodsList; public FormInventoryManagement() { InitializeComponent(); InitializeUI(); LoadInventoryData(); } private void InitializeUI() { // 设置窗口标题 Text = "库存管理"; // 设置窗口大小 Width = 600; Height = 500; // 设置窗口起始位置为屏幕中央 StartPosition = FormStartPosition.CenterScreen; // 创建用于显示库存信息的DataGridView(可根据实际需求添加更多列显示详细信息) dataGridViewInventory = new DataGridView(); dataGridViewInventory.Location = new Point(20, 20); dataGridViewInventory.Width = 560; dataGridViewInventory.Height = 320; dataGridViewInventory.Columns.Add("GoodsName", "商品名称"); dataGridViewInventory.Columns.Add("StockQuantity", "库存数量"); // 创建刷新库存按钮 Button buttonRefresh = new Button(); buttonRefresh.Text = "刷新库存"; buttonRefresh.Location = new Point(20, 350); buttonRefresh.Click += buttonRefresh_Click; // 创建用于保存库存修改的按钮 Button buttonSaveInventory = new Button(); buttonSaveInventory.Text = "保存库存修改"; buttonSaveInventory.Location = new Point(20, 400); // 可根据界面布局调整位置 buttonSaveInventory.Click += buttonSaveInventory_Click; // 关联按钮点击事件 // 将各控件添加到库存管理窗口 Controls.Add(dataGridViewInventory); Controls.Add(buttonRefresh); Controls.Add(buttonSaveInventory); } private void LoadInventoryData() { goodsList = LoadGoods(); LoadInventoryRecords(); } private List<Goods> LoadGoods() { List<Goods> result = new List<Goods>(); string connectionString = "Data Source=LAPTOP-ODQTAPDG\\MSSQLSERVER01;Initial Catalog=SnacksInventoryDB;User ID=root;Password=123456"; using (SqlConnection connection = new SqlConnection(connectionString)) { string query = "SELECT Goods_id, Goods_name, Unit_price, Category FROM Goods"; SqlCommand command = new SqlCommand(query, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { int id = (int)reader["Goods_id"]; string name = reader["Goods_name"].ToString(); decimal price = (decimal)reader["Unit_price"]; string category = reader["Category"].ToString(); result.Add(new Goods(id, name, price, category)); } reader.Close(); } catch (Exception ex) { MessageBox.Show("加载商品数据出错:" + ex.Message); } } return result; } private void buttonSaveInventory_Click(object sender, EventArgs e) { string connectionString = "Data Source=LAPTOP-ODQTAPDG\\MSSQLSERVER01;Initial Catalog=SnacksInventoryDB;User ID=root;Password=123456"; using (SqlConnection connection = new SqlConnection(connectionString)) { try { connection.Open(); // 遍历库存管理页面中展示库存数据的控件(假设使用DataGridView控件展示库存,名为dataGridViewInventory) foreach (DataGridViewRow row in dataGridViewInventory.Rows) { if (!row.IsNewRow) { int goodsId = Convert.ToInt32(row.Cells["goods_id"].Value); // 获取商品ID,将列名修改为goods_id,需确保与实际一致 int newStockQuantity = Convert.ToInt32(row.Cells["StockQuantity"].Value); // 获取修改后的库存数量,同样确保列名正确 // 更新库存的SQL语句 string updateQuery = "UPDATE inventory SET stock_quantity = @NewStockQuantity WHERE goods_id = @GoodsId"; SqlCommand updateCommand = new SqlCommand(updateQuery, connection); updateCommand.Parameters.AddWithValue("@NewStockQuantity", newStockQuantity); updateCommand.Parameters.AddWithValue("@GoodsId", goodsId); // 执行更新语句 updateCommand.ExecuteNonQuery(); } } MessageBox.Show("库存修改已成功保存!"); } catch (Exception ex) { MessageBox.Show("保存库存修改时出错:" + ex.Message); } } } private void FormInventoryManagement_Load(object sender, EventArgs e) { string connectionString = "Data Source=LAPTOP-ODQTAPDG\\MSSQLSERVER01;Initial Catalog=SnacksInventoryDB;User ID=root;Password=123456"; using (SqlConnection connection = new SqlConnection(connectionString)) { string query = "SELECT g.Goods_id AS GoodsId, i.stock_quantity AS StockQuantity " + "FROM goods g " + "JOIN inventory i ON g.Goods_id = i.goods_id"; SqlCommand command = new SqlCommand(query, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); dataGridViewInventory.Rows.Clear(); while (reader.Read()) { int goodsId = (int)reader["GoodsId"]; int stockQuantity = (int)reader["StockQuantity"]; dataGridViewInventory.Rows.Add(goodsId, stockQuantity); } reader.Close(); // 设置库存数量列可编辑(关键步骤,确保用户能修改库存数量) dataGridViewInventory.Columns["StockQuantity"].ReadOnly = false; } catch (Exception ex) { MessageBox.Show("加载库存数据出错:" + ex.Message); } } } private void LoadInventoryRecords() { dataGridViewInventory.Rows.Clear(); string connectionString = "Data Source=LAPTOP-ODQTAPDG\\MSSQLSERVER01;Initial Catalog=SnacksInventoryDB;User ID=root;Password=123456"; using (SqlConnection connection = new SqlConnection(connectionString)) { string query = "SELECT g.goods_name, i.stock_quantity " + "FROM goods g " + "JOIN inventory i ON g.goods_id = i.goods_id"; SqlCommand command = new SqlCommand(query, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string goodsName = reader["goods_name"].ToString(); int stockQuantity = (int)reader["stock_quantity"]; dataGridViewInventory.Rows.Add(goodsName, stockQuantity); } reader.Close(); } catch (Exception ex) { MessageBox.Show("加载库存记录出错:" + ex.Message); } } } private void buttonRefresh_Click(object sender, EventArgs e) { LoadInventoryRecords(); } private class Goods { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public string Category { get; set; } public Goods(int id, string name, decimal price, string category) { Id = id; Name = name; Price = price; Category = category; } } private void LoadGoodsData() { goodsList = LoadGoods(); foreach (Goods good in goodsList) { // 这里可以根据实际需求,对商品数据做更多处理,比如构建用于显示的格式化字符串等 // 暂时只是简单示例将商品名称添加到列表等操作 // 假设后续可能有根据商品筛选库存等功能,提前加载商品数据做准备 } } private DataGridView dataGridViewInventory; } }
namespace SnacksInventorySystem { partial class FormInventoryManagement { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(600, 400); this.IsMdiContainer = false; this.MainMenuStrip = null; this.Name = "FormInventoryManagement"; this.Text = "库存管理"; this.Load += new System.EventHandler(this.FormInventoryManagement_Load); } #endregion } }