一:截图
二:OperationLogDAL.cs源代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using HRMSys.Model;using System.Data;using System.Data.SqlClient;namespace HRMSys.DAL{ public class OperationLogDAL { /// /// 将数据库的格式转换为object对象的字段格式 /// /// /// public OperationLog Tomodel(DataRow row) { OperationLog log=new OperationLog(); log.Id=(Guid)row["Id"]; log.OperatorId = (Guid)row["OperatorId"]; log.MakeDate=(DateTime)row["MakeDate"]; log.ActionDesc = (string)row["ActionDesc"]; log.LoginUser = (string)sqlhelper.FromDbValue(row["LoginUser"]); return log; } /// /// 插入一条操作记录 /// /// /// public void InsertLog(Guid OperatorId, string loginUser, string ActionDesc) { sqlhelper.ExecuteNon(@"insert into T_OperationLog(Id,OperatorId,MakeDate,ActionDesc,LoginUser) values(newid(),@OperatorId,getdate(),@ActionDesc,@LoginUser)", new SqlParameter("@ActionDesc", ActionDesc), new SqlParameter("@OperatorId", OperatorId), new SqlParameter("@LoginUser", loginUser));//这里是values,不是value } /// /// 插入一条操作记录 /// /// /// public void InsertLog(Guid OperatorId, string ActionDesc) { sqlhelper.ExecuteNon(@"insert into T_OperationLog(Id,OperatorId,MakeDate,ActionDesc) values(newid(),@OperatorId,getdate(),@ActionDesc)", new SqlParameter("@ActionDesc", ActionDesc), new SqlParameter("@OperatorId", OperatorId) );//这里是values,不是value } /// /// 得到综合查询的OperationLog数组 /// /// /// /// public OperationLog[] Search(string sql, SqlParameter[] sqlparameters) { DataTable table = sqlhelper.datatable(sql, sqlparameters); //OperationLog[] log=new OperationLog[table.Rows.Count](); OperationLog[] log = new OperationLog[table.Rows.Count];//不需要再加小括号了 for (int i = 0; i < table.Rows.Count; i++) { log[i] =Tomodel( table.Rows[i]); } return log; } }}
三:OperationLog.cs源代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HRMSys.Model{ public class OperationLog { public Guid Id { get; set; } public Guid OperatorId { get; set; } public DateTime MakeDate { get; set; } public String ActionDesc { get; set; } public string LoginUser { get; set; } }}
四:OperationLogList.cs源代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HRMSys.Model{ public class OperationLogList { public Guid Id { get; set; } public string OperatorName { get; set; } public DateTime MakeDate { get; set; } public String ActionDesc { get; set; } public string LoginUser { get; set; } }}
五:OperationSearchLog.cs界面和源代码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using HRMSys.DAL;using System.Data.SqlClient;using HRMSys.Model;namespace HYMSys.UI.SystemMgr{ public partial class OperationSearchLog : Form { public OperationSearchLog() { InitializeComponent(); } /// /// 载入事件,给窗口填值 /// /// /// private void OperationSearchLog_Load(object sender, EventArgs e) { OperatorDAL dal = new OperatorDAL(); Operator op = new Operator(); cb_operator.DataSource = dal.GetListNotDelete(); //cb_operator.SelectedValue = op.Id; //cb_operator.ValueMember = op.UserName; //cb_operator.SelectedValue = "Id"; //cb_operator.ValueMember = "UserName"; cb_operator.ValueMember = "Id"; cb_operator.DisplayMember = "UserName"; dtp_startdate.Value = DateTime.Today.AddDays(-5); dtp_enddate.Value = DateTime.Today; } /// /// 综合查询 /// /// /// private void btn_search_Click(object sender, EventArgs e) { List whereList = new List (); List parametersList = new List (); //if(chb_operator.CheckState==true)错误的 if(chb_operator.Checked==true) { whereList.Add("OperatorId=@OperatorId"); parametersList.Add(new SqlParameter("@OperatorId", cb_operator.SelectedValue));//不能用cb_operator.text } if (chb_date.Checked == true) { whereList.Add("MakeDate>=@dtp_startdate and MakeDate<=@dtp_enddate"); parametersList.Add(new SqlParameter("@dtp_startdate", dtp_startdate.Value));//不能用dtp_startdate parametersList.Add(new SqlParameter("@dtp_enddate", dtp_enddate.Value)); } if (chb_desc.Checked == true) { whereList.Add("ActionDesc like @ActionDesc"); parametersList.Add(new SqlParameter("@ActionDesc","%"+tb_desc.Text+"%")); } if (whereList.Count <= 0) { MessageBox.Show("至少选择一个查询条件");//防止查询出的结果过多 return; } string whereSql = string.Join(" and ", whereList); string sql = "select * from T_OperationLog " ; if (whereSql.Length > 0) { sql = sql + " where " + whereSql; } OperationLogDAL dal = new OperationLogDAL(); dgv_logResult.DataSource = dal.Search(sql, parametersList.ToArray()); // .DataSource = dal.Search(Sql, parameter); } }}