C# WindowsForm 员工管理系统四【发放工资】

要实现“发放工资”的功能,首先数据库中应该有个表用来存储工资数据,其次发放工资是由管理员执行。

新建“Pay”表

注: 因为一个人会有多条记录,比如一月的工资和二月的工资,这两条记录所属同一个人,所以ID不为主键。

这里写图片描述

这里的FinalPay=[BasePay]+[WealPay])+[RewardPay])-[UnInsurance])-[HousingFund])

这里写图片描述

添加触发器完成三表同步删除,前面已经完成了StaffInfo和StaffAccount
现在添加StaffAccount和Pay表的删除触发器

ALTER trigger [dbo].[trigger_deleteStaff2]
on [dbo].[StaffAccount]
after delete
AS
begin delete Pay
where
ID in(
select ID from deleted)
end

设计窗体

修改ManagerForm窗体,新增控件有(Button,Label,TextBox,GroupBox,)
这里写图片描述

双击“发放工资”按钮

private void button2_Click(object sender, EventArgs e)
        {
            float basePay;
            float wealPay;
            float rewardPay;
            float unInsurance;
            float housingFund;
            try {
                basePay = (float)Convert.ToDouble(txtBasePay.Text.Trim());
                wealPay = (float)Convert.ToDouble(txtWealPay.Text.Trim());
                rewardPay = (float)Convert.ToDouble(txtRewardPay.Text.Trim());
                unInsurance = (float)Convert.ToDouble(txtUnInsurance.Text.Trim());
                housingFund = (float)Convert.ToDouble(txtHousingFund.Text.Trim());
            }
            catch(Exception ex)
            {
                txtBasePay.Text = "";
                MessageBox.Show("工资不是数字,警告!!");
                return;
            }
            if (dgvManager.CurrentRow.Selected)
            {
                int id = Convert.ToInt32(dgvManager.CurrentRow.Cells[0].Value.ToString());
                string today = DateTime.Now.ToLocalTime().ToString();
                SqlParameter[] param =
                {
                    new SqlParameter("@basePay",basePay),
                    new SqlParameter("@wealPay",wealPay),
                    new SqlParameter("@rewardPay",rewardPay),
                    new SqlParameter("@unInsurance",unInsurance),
                    new SqlParameter("@housingFund",housingFund),
                    new SqlParameter("@date",today),
                    new SqlParameter("@id",id)
                };
                string sql = "insert into Pay(BasePay,WealPay,RewardPay,UnInsurance,HousingFund,Date,ID) values(@basePay,@wealPay,@rewardPay,@unInsurance,@housingFund,@date,@id)";
                string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication6.Properties.Settings.staffConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(connstr);
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddRange(param);
                int n = 0;
                n = cmd.ExecuteNonQuery();
                if (n == 0)
                {
                    MessageBox.Show("添加失败!");
                    return;
                }
                else if (n > 0)
                {
                    MessageBox.Show("添加成功!");
                }
                conn.Close();
            }
        }

双击“返回主界面”按钮,添加代码:

            this.Close();

双击“退出系统”按钮,添加代码:

            Application.Exit();

运行结果:
这里写图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页