您的位置:首页 > 编程语言 > C#

C#桌面办公应用-工资管理系统系列六

2016-07-06 21:18 666 查看
C#桌面办公应用-工资管理系统系列六

接前文系列五,本文将讲解自主开发的工资管理系统中的员工管理模块:主要包括“员工初始化信息加载”,“员工信息综合查询与分页查询”,“添加员工”,“修改员工信息”,“删除员工信息”;涉及到的技术包括:对SQL进行增删改操作,综合模糊查询,分页查询,图片的上传预览保存查看技术,18位身份证验证算法的实现与应用技术,将查询得到的结果集绑定到comboBox跟dataGridView中等技术。

若有朋友想索取我这自主开发的工资管理系统源码或者开发文档,可以加我QQ:1974544863进行交流,我愿意视情况出售给你,这个系统也可以当做毕业设计系统!当然了,目的主要还是希望能与诸位交流!

本文先介绍员工管理模块中的“员工初始化信息加载”,开发步骤是这样的:设计员工model,在数据库建立相应的员工表,设计并实现员工管理初始化界面,初始化查询绑定员工数据到dataGridView中,单击dataGridView中的某一行可以查看改行对应的员工的详细数据。下面将逐个实现该小模块的功能!

首先是员工表基本字段的建立,如下图所示:



接着是界面的设计与实现,如下图所示:



上图即为员工管理中初始化加载时候的界面:第一部分是“分页查询、增删改、综合模糊查询”;第二部分是“员工信息列表-采用dataGridView控件进行展示”;第三部分是“员工详细信息-各种基本控件以及图片展示空间pitureBox”。

紧接着是“初始化查询绑定员工数据到dataGridView”:主要是窗体的load事件:

//窗体初始化加载事件
private void frmEmployee_Load(object sender, EventArgs e)
{
cmmPage.PageNo = 1;
cmmPage.PageSize = pageSize;
try
{
String employeeSQL = "select top "+cmmPage.PageSize+" empId as '员工编号',loginName as '登录用户名',powerName as '用户权限',empName as '员工姓名',age as '年龄',sex as '性别',partName as '所属部门',idCardNo as '身份证号',jobType as '职位类型',jobDate as '入职时间',imagePosition as '图片位置',tb_employee.memo as '备注信息' from tb_employee,tb_powerType,tb_part where tb_employee.powerId=tb_powerType.powerId and tb_employee.partID=tb_part.partID and empId not in (select top "+cmmPage.Start+" empId from tb_employee order by empId)order by empId";

recordCount= employeeService.getCount("select COUNT(*) from tb_employee");
pageCount = commonMessage.getTotalPage(pageSize, recordCount);

bindPaginationQuery(employeeSQL,cmmPage,recordCount,pageCount,sender, e);
this.textBoxCurrentPage.Text = "1";
}
catch (System.Exception ex)
{
MessageBox.Show("初始化加载员工信息出错: \n" + ex.Message);
}
}


其中,cmmPage是分页工具类的实例,需要在窗体构造函数之后创建,除此之外,还有employeeService也是需要

在窗体构造函数之后创建:

private EmployeeService employeeService = new EmployeeService();
private CommonMessage commonMessage = new CommonMessage();
private CommonUtils cmmUtils = new CommonUtils();
其中,employeeService代码如下,其代码需要调用DBOperate即Dao层的代码和CommonUtils工具类的代码,即所谓的MVC逐层调用!而DBOperate和CommonUtils中因为涉及到我自主开发的核心技术,所以在这里就不在贴出来,当然了,在前面已经有贴出一小部分了!如果你想获取我没贴出来的(还是有很多没贴出来的

)代码,可以加我上面提供的QQ与我交流,我愿意出售给你!

//员工服务层
class EmployeeService
{
private DBOperate operate = new DBOperate();
private CommonUtils cmmUtils = new CommonUtils();

/// <summary>
/// 绑定Sql语句查询的结果到dataGridView中
/// </summary>
/// <param name="sql"></param>
/// <param name="dataGridView"></param>
public void bindSqlResultToDatagridView(DataGridView dataGridView, String sql)
{
operate.BindDataGridView(dataGridView, sql);
#region MyRegion //设置好datagriview中的列的显示宽度
dataGridView.Columns[0].Width = 140;
dataGridView.Columns[1].Width = 160;
dataGridView.Columns[2].Width = 160;
dataGridView.Columns[3].Width = 140;
dataGridView.Columns[4].Width = 140;
dataGridView.Columns[5].Width = 140;
dataGridView.Columns[6].Width = 180;
dataGridView.Columns[7].Width = 180;
dataGridView.Columns[8].Width = 180;
dataGridView.Columns[9].Width = 180;
dataGridView.Columns[10].Width = 120;
dataGridView.Columns[11].Width = 220;
#endregion
}

/// <summary>
/// //绑定SQL查询语句中的指定列到下拉列表
/// </summary>
/// <param name="strTable">数据库表名</param>
/// <param name="cb">ComboBox对象</param>
/// <param name="i">指定数据列索引</param>
public void bindSpecificColumnToComboBox(String strSQL, ComboBox cb)
{
operate.BindDropdownlist(strSQL, cb, 0);
}

/// <summary>
/// 用于判断指定的字段值 是否已经存在
/// </summary>
/// <param name="strSQL">其中sql语句为:select count(columnName) from ...格式</param>
/// <returns></returns>
public Boolean isExistSpecificObject(String strSQL)
{
Boolean res = false;
int i = operate.HumanNum(strSQL);
if (i > 0)
{
res = true;
}
return res;
}

/// <summary>
/// 根据SQL获取数量值
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public int getCount(String strSQL)
{
return operate.HumanNum(strSQL);
}

/// <summary>
/// 保存或者更新(删除,修改)员工信息
/// </summary>
/// <param name="strSQL"></param>
public int saveOrUpdatePart(String strSQL)
{
int i = operate.OperateData(strSQL);
Console.WriteLine("操作的结果: " + i);
return i;
}

/// <summary>
/// 获取指定的数据库某一字段的值
/// </summary>
/// <param name="strObjectSQL">查询的SQL--指定了要查询的数据库字段</param>
/// <param name="fieldNames">数据库字段数组</param>
/// <returns></returns>
public String getSpecificColumnValue(String strObjectSQL, String[] fieldNames)
{
return operate.GetDatasFromSelectedTable(strObjectSQL, fieldNames)[0];
}

/// <summary>
///  根据实际保存时员工相片的名称设置员工的相片的实际保存路径
/// </summary>
/// <param name="empPictureStoreName"></param>
/// <returns>实际保存路径: 项目的debug目录\images\employee</returns>
public String setEmpImageRealStoreLocation(String empPictureStoreName)
{
String empImgStoreDestPath = Application.StartupPath.ToString() + "\\images\\employee\\" + empPictureStoreName;
return empImgStoreDestPath;
}

/// <summary>
/// 读取员工相片
/// </summary>
/// <param name="empImageName">数据库存储的员工的相片名:imagePosition</param>
/// <param name="myImage"></param>
public void readEmpImage(String empImageName, PictureBox myImage)
{
String imageLocation = Application.StartupPath.ToString() + "\\images\\employee\\";
try
{
String imageRealPath = imageLocation + empImageName;
myImage.Image = Image.FromFile(imageRealPath);
}
catch (System.Exception ex)
{
String imageRealPath = imageLocation + "default.jpg";
myImage.Image = Image.FromFile(imageRealPath);
Console.WriteLine("读取员工相片发生异常: " + ex.Message);
}
}

/// <summary>
/// 移除文件夹下指定的相片:当然只有上传了相片的才移除原先的相片
/// </summary>
/// <param name="removeImageName"></param>
public void removeImage(String removeImageName)
{
try
{
String empImageLocation = Application.StartupPath.ToString() + "\\images\\employee\\" + removeImageName;
File.Delete(@empImageLocation);
}
catch (System.Exception ex)
{
Console.WriteLine("移除员工相片发生异常: " + ex.Message);
}
}
}


在里面,我们可以看到,已经包含了将查询得到的结果集绑定到comboBox跟dataGridView中等技术,当然啦,具体的实现还是Dao那一层代码,即DBOperate类中的代码!

下图是初始化加载员工信息列表时候的效果:



下面实现:“单击dataGridView中的某一行可以查看改行对应的员工的详细数据”,其实就是dataGridView的cellMouseClick事件,源代码如下:

//鼠标单击事件
private void dataGridViewEmployeeInfo_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
textBoxYuanGongDengLuMing.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[1].Value.ToString().Trim();
textBoxYuanGongYongHuQuanXian.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[2].Value.ToString().Trim();
textBoxYuanGongXingMing.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[3].Value.ToString().Trim();
textBoxYuanGongNianNing.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[4].Value.ToString().Trim();
textBoxYuanGongXingBie.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[5].Value.ToString().Trim();
textBoxYuanGongSuoShuBuMeng.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[6].Value.ToString().Trim();
textBoxYuanGongShenFenZheng.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[7].Value.ToString().Trim();
textBoxYuanGongZhiWeiLeiXing.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[8].Value.ToString().Trim();
dateTimePickerYuanGongRuZhi.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[9].Value.ToString().Trim();
textBoxYuanGongBeiZhu.Text = dataGridViewEmployeeInfo.CurrentRow.Cells[11].Value.ToString().Trim();

//iamgePosition要不是null:没上传图片 要不就是"位置信息"
String imagePosition = dataGridViewEmployeeInfo.CurrentRow.Cells[10].Value.ToString().Trim();
if (imagePosition=="")
{
imagePosition = "default.jpg";

}
employeeService.readEmpImage(imagePosition, EmpPictureBox);

}
catch(System.Exception ex)
{
employeeService.readEmpImage("default.jpg", EmpPictureBox);
Console.WriteLine("鼠标单击发生异常: " + ex.Message);
}
}


在上面,其实涉及到“C#读取图片并显示在控件pitureBox”技术:
<span style="color:#ff0000;">employeeService.readEmpImage(imagePosition, EmpPictureBox);</span>
这是由employeeService服务类中提供的,当然啦,底层的实现还是有DBOperate实现的,即Dao层的代码!

好了,把效果贴出来吧:



好了,这一文就到这里吧!下来两篇博文就讲讲C#在winform下如何实现分页查询(很有用的哦

)、综合模糊查询以及18位身份证号码验证算法的原理以及C#实现!

欢迎各位与我交流!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息