设计数据库表T_OperationLog。如下:
在MOdel中建一个T_OperationLog类,代码如下;
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class T_OperationLog { public System.Guid Id { get; set; } public System.Guid OperatorId { get; set; } public System.DateTime MakeDate { get; set; } public System.String ActionDesc { get; set; } } }
第二步:在DAL中新建一个T_OperationLogDAL类,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using HRMSys.Model; using System.Data.SqlClient; using System.Data; namespace HRMSys.DAL { public class T_OperationLogDAL { private T_OperationLog ToModel(DataRow row) { T_OperationLog model = new T_OperationLog(); model.Id = (System.Guid)SqlHelper.FromDbValue(row["Id"]); model.OperatorId = (System.Guid)SqlHelper.FromDbValue(row["OperatorId"]); model.MakeDate = (System.DateTime)SqlHelper.FromDbValue(row["MakeDate"]); model.ActionDesc = (string)SqlHelper.FromDbValue(row["ActionDesc"]); return model; } public void Insert(Guid operatorId, string actionDesc) { SqlHelper.ExecuteNonQuery(@"insert into T_OperationLog( Id,OperatorId,MakeDate,ActionDesc) values(newid(),@OperatorId,getdate(),@ActionDesc)", new SqlParameter("@OperatorId", operatorId) , new SqlParameter("@ActionDesc", actionDesc)); } public T_OperationLog[] Search(string sql, SqlParameter[] parameters) { DataTable table = SqlHelper.ExecuteDataTable(sql, parameters); T_OperationLog[] logs = new T_OperationLog[table.Rows.Count]; for (int i = 0; i < table.Rows.Count; i++) { logs[i] = ToModel(table.Rows[i]); } return logs; } } }
第三步:因为在登录时候要记录登陆的相关信息,所以在LoginWindown的login_Click事件中if语句和else中添加代码
new T_OperationLogDAL().Insert(op.Id,"登陆成功!");
new T_OperationLogDAL().Insert(op.Id,"登陆不成功!");
在管理者的UI页面即OperatorListUI.cs中删除操作代码内添加代码
new T_OperationLogDAL().Insert(*****, "删除管理员"+op.UserName);//****是记录的是操作者的ID
因为要记录是谁进行删除的删除操作,使用亦需要在登陆界面中的LoginWindown的login_Click事件中添加代码如下:
//把登录操作者的Id保存到全局的“Session”
//存到Application.Current.Properties里面的在程序其他地方也可以取
Application.Current.Properties["OperatorId"] = op.Id;
在在管理者的UI页面即OperatorListUI.cs中删除操作代码内添加代码如下:取得操作者的ID
//因为每次都在进行操作时候这样写比较麻烦。所以封装成方法,存在commonHelper.cs中
// Guid operatorId = (Guid)Application.Current.Properties["OperatorId"];
Guid operatorId = CommonHelper.GetOperatorId();
new T_OperationLogDAL().Insert(operatorId, "删除管理员成功"+op.UserName);
在CommonHelper.cs中添加代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace HRMSys.UI { public class CommonHelper { /// <summary> /// 获得登录用户的Id /// </summary> /// <returns></returns> public static Guid GetOperatorId() { Guid operatorId = (Guid)Application.Current.Properties["OperatorId"]; return operatorId; } } }
在EmployeeEditWindow新增员工界面页添加如下代码:
Guid operatorId = CommonHelper.GetOperatorId();
new T_OperationLogDAL().Insert(operatorId, "新增职员" + employee.Name);
修改员工时候:
Guid operatorId = CommonHelper.GetOperatorId();
new T_OperationLogDAL().Insert(operatorId, "修改职员" + employee.Name);
在操作员管理界面即OperatorEditorWindow中添加,删除,修改操作员中添加代码如下:
Guid operatorId = CommonHelper.GetOperatorId();
new T_OperationLogDAL().Insert(operatorId, "新增操作员" + txtUserName.Text);
Guid operatorId = CommonHelper.GetOperatorId();
new T_OperationLogDAL().Insert(operatorId, "修改操作员" + txtUserName.Text);