C#员工薪资管理系统代码
时间: 2023-08-02 10:35:28 浏览: 387
以下是一个简单的C#员工薪资管理系统的代码示例,供参考:
1.数据库连接和实体类定义
```csharp
using System.Data.Entity;
// 定义员工、工资和科室的实体类
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
public string Department { get; set; }
public string Position { get; set; }
public virtual Salary Salary { get; set; }
}
public class Salary
{
public int Id { get; set; }
public int BasicSalary { get; set; }
public int Bonus { get; set; }
public int Allowance { get; set; }
public int Deduction { get; set; }
public int TotalSalary { get; set; }
public virtual Employee Employee { get; set; }
}
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
// 数据库连接
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Salary> Salaries { get; set; }
public DbSet<Department> Departments { get; set; }
}
```
2.数据录入和修改
```csharp
using System.Linq;
// 添加员工信息
private void AddEmployee(Employee employee)
{
using (var db = new EmployeeContext())
{
db.Employees.Add(employee);
db.SaveChanges();
}
}
// 更新员工信息
private void UpdateEmployee(Employee employee)
{
using (var db = new EmployeeContext())
{
var e = db.Employees.Find(employee.Id);
if (e != null)
{
e.Name = employee.Name;
e.Gender = employee.Gender;
e.Age = employee.Age;
e.Department = employee.Department;
e.Position = employee.Position;
db.SaveChanges();
}
}
}
// 加载员工列表
private void LoadEmployees()
{
using (var db = new EmployeeContext())
{
var employees = db.Employees.Include(e => e.Salary).ToList();
dataGridViewEmployees.DataSource = employees;
}
}
// 保存员工信息
private void buttonSave_Click(object sender, EventArgs e)
{
var employee = new Employee
{
Name = textBoxName.Text,
Gender = comboBoxGender.Text,
Age = int.Parse(textBoxAge.Text),
Department = comboBoxDepartment.Text,
Position = comboBoxPosition.Text,
Salary = new Salary
{
BasicSalary = int.Parse(textBoxBasicSalary.Text),
Bonus = int.Parse(textBoxBonus.Text),
Allowance = int.Parse(textBoxAllowance.Text),
Deduction = int.Parse(textBoxDeduction.Text)
}
};
// 根据ID判断是添加还是更新操作
if (string.IsNullOrEmpty(textBoxId.Text))
{
AddEmployee(employee);
}
else
{
employee.Id = int.Parse(textBoxId.Text);
UpdateEmployee(employee);
}
LoadEmployees();
}
```
3.计算工资和统计分析
```csharp
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
// 计算员工工资
private void CalculateSalary(Employee employee)
{
var salary = employee.Salary;
salary.TotalSalary = salary.BasicSalary + salary.Bonus + salary.Allowance - salary.Deduction;
}
// 加载统计信息
private void LoadStatistics()
{
using (var db = new EmployeeContext())
{
var departments = db.Departments.Include(d => d.Employees.Select(e => e.Salary));
var statistics = new List<Statistic>();
foreach (var department in departments)
{
var employees = department.Employees;
var count = employees.Count();
var totalSalary = employees.Sum(e => e.Salary.TotalSalary);
var statistic = new Statistic
{
Department = department.Name,
Count = count,
TotalSalary = totalSalary
};
statistics.Add(statistic);
}
dataGridViewStatistics.DataSource = statistics;
}
}
// 统计信息实体类
public class Statistic
{
public string Department { get; set; }
public int Count { get; set; }
public int TotalSalary { get; set; }
}
```
4.数据删除和查询
```csharp
using System.Data.Entity;
using System.Linq;
// 删除员工信息
private void DeleteEmployee(int employeeId)
{
using (var db = new EmployeeContext())
{
var employee = db.Employees.Find(employeeId);
if (employee != null)
{
db.Employees.Remove(employee);
db.SaveChanges();
}
}
}
// 查询员工信息
private void SearchEmployees(string keyword)
{
using (var db = new EmployeeContext())
{
var employees = db.Employees.Include(e => e.Salary).Where(e => e.Name.Contains(keyword)).ToList();
dataGridViewEmployees.DataSource = employees;
}
}
// 删除员工按钮点击事件
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridViewEmployees.SelectedRows.Count > 0)
{
var employeeId = int.Parse(dataGridViewEmployees.SelectedRows[0].Cells[0].Value.ToString());
DeleteEmployee(employeeId);
LoadEmployees();
}
}
// 查询员工按钮点击事件
private void buttonSearch_Click(object sender, EventArgs e)
{
var keyword = textBoxKeyword.Text;
SearchEmployees(keyword);
}
```
以上是一个简单的C#员工薪资管理系统的代码示例,具体实现细节可以根据实际业务需求进行设计。
阅读全文