11.18金额管理页面

using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using System.Drawing;

namespace SnacksInventorySystem
{
    public partial class FormAmountManagement : Form
    {
        public FormAmountManagement()
        {
            InitializeComponent();
            InitializeUI();
        }

        private TextBox textBoxPurchaseTotal; // 将textBoxPurchaseTotal定义为类的成员变量
        private TextBox textBoxProfit; // 将textBoxProfit定义为类的成员变量
        private TextBox textBoxSalesTotal;
        private void InitializeUI()
        {
            // 设置窗口标题
            Text = "金额管理";
            // 设置窗口大小
            Width = 600;
            Height = 400;
            // 设置窗口起始位置为屏幕中央
            StartPosition = FormStartPosition.CenterScreen;

            // 创建用于显示采购支出总额的标签和文本框
            Label labelPurchaseTotal = new Label();
            labelPurchaseTotal.Text = "采购支出总额:";
            labelPurchaseTotal.Location = new Point(20, 20);
            labelPurchaseTotal.AutoSize = true;

            textBoxPurchaseTotal = new TextBox(); // 正确实例化文本框控件
            textBoxPurchaseTotal.Location = new Point(150, 20);
            textBoxPurchaseTotal.ReadOnly = true;
            Controls.Add(textBoxPurchaseTotal);
          
        

            // 创建用于显示销售总收入总额的标签和文本框
            Label labelSalesTotal = new Label();
            labelSalesTotal.Text = "销售总收入总额:";
            labelSalesTotal.Location = new Point(20, 60);
            labelSalesTotal.AutoSize = true;

            textBoxSalesTotal = new TextBox();
            textBoxSalesTotal.Location = new Point(150, 60);
            textBoxSalesTotal.ReadOnly = true;

            // 创建用于显示利润(销售总收入 - 采购支出)的标签和文本框
            Label labelProfit = new Label();
            labelProfit.Text = "利润:";
            labelProfit.Location = new Point(20, 100);
            labelProfit.AutoSize = true;

           textBoxProfit = new TextBox();
            textBoxProfit.Location = new Point(150, 100);
            textBoxProfit.ReadOnly = true;

            // 创建查询按钮
            Button buttonQuery = new Button();
            buttonQuery.Text = "查询";
            buttonQuery.Location = new Point(20, 140);
            buttonQuery.Click += buttonQuery_Click;

            // 将各控件添加到金额管理页面
            Controls.Add(labelPurchaseTotal);
            Controls.Add(textBoxPurchaseTotal);
            Controls.Add(labelSalesTotal);
            Controls.Add(textBoxSalesTotal);
            Controls.Add(labelProfit);
            Controls.Add(textBoxProfit);
            Controls.Add(buttonQuery);
        }
        private void FormAmountManagement_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 purchaseQuery = "SELECT SUM(total_amount) FROM purchase";
                SqlCommand purchaseCommand = new SqlCommand(purchaseQuery, connection);
                decimal purchaseTotal = 0;
                try
                {
                    connection.Open();
                    object purchaseResult = purchaseCommand.ExecuteScalar();
                    if (purchaseResult != null)
                    {
                        purchaseTotal = Convert.ToDecimal(purchaseResult);
                        textBoxPurchaseTotal.Text = purchaseTotal.ToString("F2");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载采购支出总额数据出错:" + ex.Message);
                }

                // 查询销售总收入总额
                string salesQuery = "SELECT SUM(total_amount) FROM sales";
                SqlCommand salesCommand = new SqlCommand(salesQuery, connection);
                decimal salesTotal = 0;
                try
                {
                    connection.Open();
                    object salesResult = salesCommand.ExecuteScalar();
                    if (salesResult != null)
                    {
                        salesTotal = Convert.ToDecimal(salesResult);
                        textBoxSalesTotal.Text = salesTotal.ToString("F2");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载销售总收入总额数据出错:" + ex.Message);
                }

                // 计算利润并显示
                decimal profit = salesTotal - purchaseTotal;
                textBoxProfit.Text = profit.ToString("F2");
            }
        }

        private void buttonQuery_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))
            {
                // 查询采购支出总额
                string purchaseQuery = "SELECT SUM(total_amount) FROM purchase";
                SqlCommand purchaseCommand = new SqlCommand(purchaseQuery, connection);
                decimal purchaseTotal = 0;
                try
                {
                    connection.Open();
                    object purchaseResult = purchaseCommand.ExecuteScalar();
                    if (purchaseResult != null)
                    {
                        purchaseTotal = Convert.ToDecimal(purchaseResult);
                        textBoxPurchaseTotal.Text = purchaseTotal.ToString("F2");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("查询采购支出总额出错:" + ex.Message);
                }

                // 查询销售总收入总额
                string salesQuery = "SELECT SUM(total_amount) FROM sales";
                SqlCommand salesCommand = new SqlCommand(salesQuery, connection);
                decimal salesTotal = 0;
                try
                {
                    connection.Open();
                    object salesResult = salesCommand.ExecuteScalar();
                    if (salesResult != null)
                    {
                        salesTotal = Convert.ToDecimal(salesResult);
                        textBoxSalesTotal.Text = salesTotal.ToString("F2");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("查询销售总收入总额出错:" + ex.Message);
                }

                // 计算利润并显示
                decimal profit = salesTotal - purchaseTotal;
                textBoxProfit.Text = profit.ToString("F2");
            }
        }
    }
}
namespace SnacksInventorySystem
{
    partial class FormAmountManagement
    {
        /// <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 = "FormAmountManagement";
            this.Text = "金额管理";
            this.Load += new System.EventHandler(this.FormAmountManagement_Load);
        }

        #endregion
    }
}

 

posted @ 2025-01-02 15:55  jais  阅读(14)  评论(0)    收藏  举报