综合查询员工和datetime.now和datetime.today区别
一:综合查询图
二:EmployeeListWindow.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 HRMSys.Model;
using System.Data.SqlClient; namespace HYMSys.UI.EmployeeMgr
{
public partial class EmployeeListWindow : Form
{
public EmployeeListWindow()
{
InitializeComponent();
}
/// <summary>
/// 加载所有信息
/// </summary>
public EmployeeList[] load()
{
EmployeeDAL de = new EmployeeDAL(); return de.ListAll(); } /// <summary>
/// 添加员工
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolsb_add_Click(object sender, EventArgs e)
{
EmployeeEditWindow edit = new EmployeeEditWindow();
edit.IsAdd = true;
edit.ShowDialog(); if (edit.IsLoad == true)
{
dataGridView1.DataSource = load();
} }
/// <summary>
/// 删除员工
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolsb_delete_Click(object sender, EventArgs e)
{
Guid id=(Guid)dataGridView1.CurrentRow.Cells[].Value;
string name=(string)dataGridView1.CurrentRow.Cells[].Value;//这里的cell是相对于list员工操作窗口上name这列,即使第二行
if (MessageBox.Show("真的要删除---"+name+"---吗?","警告!",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
EmployeeDAL dal = new EmployeeDAL();
dal.DeleteById(id);
dataGridView1.DataSource = load();
} }
/// <summary>
/// 修改员工信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolsb_edit_Click(object sender, EventArgs e)
{
EmployeeEditWindow edit = new EmployeeEditWindow();
edit.EditId = (Guid)dataGridView1.CurrentRow.Cells[].Value;
edit.IsAdd = false;
edit.ShowDialog();
if (edit.IsLoad == true)
{
dataGridView1.DataSource = load();
} }
/// <summary>
/// 自动加载所有员工信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EmployeeListWindow_Load(object sender, EventArgs e)
{
DepartmentDAL dal = new DepartmentDAL();
cb_depart.DataSource = dal.ListAll();
cb_depart.DisplayMember = "Name";
cb_depart.ValueMember = "Id";
//cb_depart.SelectedValue= dtp_indate.Value = DateTime.Today.AddMonths(-);
dtp_enddate.Value = DateTime.Today; dataGridView1.DataSource= load(); }
/// <summary>
/// 综合查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
List<string> wherelist = new List<string>();
List<SqlParameter> parameter = new List<SqlParameter>();
if (ckb_name.Checked == true)
{
wherelist.Add("Name=@Name");
parameter.Add(new SqlParameter("@Name",tb_name.Text)); }
if (ckb_indate.Checked == true)
{
wherelist.Add("InDate>=@InDateStart and InDate<=@InDateEnd");
parameter.Add(new SqlParameter("@InDateStart",dtp_indate.Value));
parameter.Add(new SqlParameter("@InDateEnd",dtp_enddate.Value));
}
if (ckb_depart.Checked == true)
{
wherelist.Add("DepartmentId=@DepartmentId");
parameter.Add(new SqlParameter("@DepartmentId",cb_depart.SelectedValue));
} string whereSql=string.Join(" and ",wherelist);
string Sql="select * from T_Employee";
if (whereSql.Length > )
{
Sql = Sql + " where " + whereSql;
}
EmployeeDAL dal = new EmployeeDAL();
dataGridView1.DataSource= dal.SearchBySome(Sql, parameter); }
}
}
三:EmployeeDAL.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 EmployeeDAL
{
/// <summary>
/// 将表的列转换为EmployeeList对象的字段
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
public EmployeeList ToEmployeeListModel(DataRow row)
{
EmployeeList employee = new EmployeeList();
employee.Address = (string)row["Address"];
employee.BaseSalary = (int)row["BaseSalary"];
employee.BirthDay = (DateTime)row["BirthDay"];
employee.ContractEndDay = (DateTime)row["ContractEndDay"];
employee.ContractStartDay = (DateTime)row["ContractStartDay"];
employee.Department =GetNameByGuidDE( (Guid)row["DepartmentId"]);//////
employee.Education= GetNameByGuidID((Guid)row["EducationId"]);/////
employee.Email = (string)row["Email"];
employee.EmergencyContact = (string)sqlhelper.FromDbValue(row["EmergencyContact"]);
employee.Gender = GetNameByGuidID((Guid)row["GenderId"]);///////
employee.Id = (Guid)row["Id"];
employee.IdNum = (string)row["IdNum"];
employee.InDate = (DateTime)row["InDate"];
employee.Major = (string)sqlhelper.FromDbValue(row["Major"]);
employee.Marriage = GetNameByGuidID((Guid)row["MarriageId"]);/////////
employee.Name = (string)row["Name"];
employee.Nationality = (string)row["Nationality"];
employee.NativeAddr = (string)row["NativeAddr"];
employee.Number = (string)row["Number"];
employee.PartyStatus = GetNameByGuidID((Guid)row["PartyStatusId"]);//////
employee.Position = (string)row["Position"];
employee.Remarks = (string)sqlhelper.FromDbValue(row["Remarks"]);
employee.Resume = (string)sqlhelper.FromDbValue(row["Resume"]);
employee.School = (string)sqlhelper.FromDbValue(row["School"]);
employee.TelNum = (string)row["TelNum"];
employee.IsStopped = (bool)row["IsStopped"];
//todo:如果员工非常多,那么Photo会增加内存占用
employee.Photo = (byte[])sqlhelper.FromDbValue(row["Photo"]);
return employee;
} /// <summary>
/// 将表转换为Employee对象的字段
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
public Employee ToEmployeeModel(DataRow row)
{
Employee employee = new Employee();
employee.Address = (string)row["Address"];
employee.BaseSalary = (int)row["BaseSalary"];
employee.BirthDay = (DateTime)row["BirthDay"];
employee.ContractEndDay = (DateTime)row["ContractEndDay"];
employee.ContractStartDay = (DateTime)row["ContractStartDay"];
employee.DepartmentId = (Guid)row["DepartmentId"];//////
employee.EducationId = (Guid)row["EducationId"];/////
employee.Email = (string)row["Email"];
employee.EmergencyContact = (string)sqlhelper.FromDbValue(row["EmergencyContact"]);
employee.GenderId = (Guid)row["GenderId"];///////
employee.Id = (Guid)row["Id"];
employee.IdNum = (string)row["IdNum"];
employee.InDate = (DateTime)row["InDate"];
employee.Major = (string)sqlhelper.FromDbValue(row["Major"]);
employee.MarriageId = (Guid)row["MarriageId"];/////////
employee.Name = (string)row["Name"];
employee.Nationality = (string)row["Nationality"];
employee.NativeAddr = (string)row["NativeAddr"];
employee.Number = (string)row["Number"];
employee.PartyStatusId = (Guid)row["PartyStatusId"];//////
employee.Position = (string)row["Position"];
employee.Remarks = (string)sqlhelper.FromDbValue(row["Remarks"]);
employee.Resume = (string)sqlhelper.FromDbValue(row["Resume"]);
employee.School = (string)sqlhelper.FromDbValue(row["School"]);
employee.TelNum = (string)row["TelNum"];
employee.IsStopped = (bool)row["IsStopped"];
//todo:如果员工非常多,那么Photo会增加内存占用
employee.Photo = (byte[])sqlhelper.FromDbValue(row["Photo"]);
return employee;
}
/// <summary>
/// 软删除指定id的员工信息
/// </summary>
/// <param name="id"></param>
public void DeleteById(Guid id)
{
//update T_Operator set IsLocked=@IsLocked where Id=@Id"
int i= sqlhelper.ExecuteNon("update T_Employee set IsStopped =1 where Id=@Id",
new SqlParameter ("@Id",id)); }
/// <summary>
/// 指定id从idname中取得名字
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string GetNameByGuidID(Guid id)
{
DataTable table= sqlhelper.datatable("select Name from T_IdName where Id=@Id",
new SqlParameter("@Id",id));
DataRow row = table.Rows[];
return (string) row["Name"]; }
/// <summary>
/// 指定id从department中取得名字
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string GetNameByGuidDE(Guid id)
{
DataTable table = sqlhelper.datatable("select Name from T_Department where Id=@Id",
new SqlParameter("@Id", id));
DataRow row = table.Rows[];
return (string)row["Name"];
}
/// <summary>
/// 得到所有未删除的员工的所有信息
/// </summary>
/// <returns>Employee数组</returns>
public EmployeeList[] ListAll()
{
DataTable table = sqlhelper.datatable("select * from T_Employee where IsStopped=0");
EmployeeList[] items = new EmployeeList[table.Rows.Count];
for (int i = ; i < table.Rows.Count; i++)
{
EmployeeList employee = ToEmployeeListModel(table.Rows[i]);
items[i] = employee;
}
return items;
}
/// <summary>
/// 得到指定id的员工信息
/// </summary>
/// <param name="id"></param>
/// <returns>Employee对象</returns>
public Employee GetById(Guid id)
{
DataTable table = sqlhelper.datatable("select * from T_Employee where Id=@Id",
new SqlParameter("@Id", id));
if (table.Rows.Count == )
{
return ToEmployeeModel(table.Rows[]);
}
else
{
throw new Exception();
}
}
/// <summary>
/// 插入一条员工数据
/// </summary>
/// <param name="employee"></param>
public void Insert(Employee employee)
{
sqlhelper.ExecuteNon(@"INSERT INTO [T_Employee]
([Id],[Number],[Name],[BirthDay],[InDate],[MarriageId],[PartyStatusId],[Nationality]
,[NativeAddr],[EducationId],[Major],[School],[Address],[BaseSalary],[Email]
,[IdNum],[TelNum],[EmergencyContact],[DepartmentId],[Position],[ContractStartDay]
,[ContractEndDay],[Resume],[Remarks],[IsStopped],[GenderId],Photo)
VALUES(newid(),@Number,@Name,@BirthDay,@InDate,@MarriageId,@PartyStatusId,@Nationality
,@NativeAddr,@EducationId,@Major,@School,@Address,@BaseSalary,@Email
,@IdNum,@TelNum,@EmergencyContact,@DepartmentId,@Position,@ContractStartDay
,@ContractEndDay,@Resume,@Remarks,0,@GenderId,@Photo)", new SqlParameter("@Number", employee.Number)
, new SqlParameter("@Name", employee.Name)
, new SqlParameter("@BirthDay", employee.BirthDay)
, new SqlParameter("@InDate", employee.InDate)
, new SqlParameter("@MarriageId", employee.MarriageId)
, new SqlParameter("@PartyStatusId", employee.PartyStatusId)
, new SqlParameter("@Nationality", employee.Nationality)
, new SqlParameter("@NativeAddr", employee.NativeAddr)
, new SqlParameter("@EducationId", employee.EducationId)
, new SqlParameter("@Major", sqlhelper.ToDbValue(employee.Major))
, new SqlParameter("@School", sqlhelper.ToDbValue(employee.School))
, new SqlParameter("@Address", employee.Address)
, new SqlParameter("@BaseSalary", employee.BaseSalary)
, new SqlParameter("@Email", sqlhelper.ToDbValue(employee.Email))
, new SqlParameter("@IdNum", employee.IdNum)
, new SqlParameter("@TelNum", employee.TelNum)
, new SqlParameter("@EmergencyContact", sqlhelper.ToDbValue(employee.EmergencyContact))
, new SqlParameter("@DepartmentId", employee.DepartmentId)
, new SqlParameter("@Position", employee.Position)
, new SqlParameter("@ContractStartDay", employee.ContractStartDay)
, new SqlParameter("@ContractEndDay", employee.ContractEndDay)
, new SqlParameter("@Resume", sqlhelper.ToDbValue(employee.Resume))
, new SqlParameter("@Remarks", sqlhelper.ToDbValue(employee.Remarks))
, new SqlParameter("@GenderId", employee.GenderId)
, new SqlParameter("@Photo", SqlDbType.Image) {Value= sqlhelper.ToDbValue(employee.Photo) }//需要指定专门image的类型,数据库不认识,
); }
/// <summary>
/// 更新一条员工数据数据
/// </summary>
/// <param name="employee"></param>
public void Update(Employee employee)
{
sqlhelper.ExecuteNon(@"Update T_Employee set
[Number]=@Number,[Name]=@Name,[BirthDay]=@BirthDay,[InDate]=@InDate,
[MarriageId]=@MarriageId,[PartyStatusId]=@PartyStatusId,[Nationality]=@Nationality,
[NativeAddr]=@NativeAddr,[EducationId]=@EducationId,[Major]=@Major,[School]=@School,
[Address]=@Address,[BaseSalary]=@BaseSalary,[Email]=@Email,
[IdNum]=@IdNum,[TelNum]=@TelNum,[EmergencyContact]=@EmergencyContact,
[DepartmentId]=@DepartmentId,[Position]=@Position,[ContractStartDay]=@ContractStartDay,
[ContractEndDay]=@ContractEndDay,[Resume]=@Resume,[Remarks]=@Remarks,[GenderId]=@GenderId,
photo=@Photo Where Id=@Id", new SqlParameter("@Number", employee.Number)
, new SqlParameter("@Name", employee.Name)
, new SqlParameter("@BirthDay", employee.BirthDay)
, new SqlParameter("@InDate", employee.InDate)
, new SqlParameter("@MarriageId", employee.MarriageId)
, new SqlParameter("@PartyStatusId", employee.PartyStatusId)
, new SqlParameter("@Nationality", employee.Nationality)
, new SqlParameter("@NativeAddr", employee.NativeAddr)
, new SqlParameter("@EducationId", employee.EducationId)
, new SqlParameter("@Major", sqlhelper.ToDbValue(employee.Major))
, new SqlParameter("@School", sqlhelper.ToDbValue(employee.School))
, new SqlParameter("@Address", employee.Address)
, new SqlParameter("@BaseSalary", employee.BaseSalary)
, new SqlParameter("@Email", sqlhelper.ToDbValue(employee.Email))
, new SqlParameter("@IdNum", employee.IdNum)
, new SqlParameter("@TelNum", employee.TelNum)
, new SqlParameter("@EmergencyContact", sqlhelper.ToDbValue(employee.EmergencyContact))
, new SqlParameter("@DepartmentId", employee.DepartmentId)
, new SqlParameter("@Position", employee.Position)
, new SqlParameter("@ContractStartDay", employee.ContractStartDay)
, new SqlParameter("@ContractEndDay", employee.ContractEndDay)
, new SqlParameter("@Resume", sqlhelper.ToDbValue(employee.Resume))
, new SqlParameter("@Remarks", sqlhelper.ToDbValue(employee.Remarks))
, new SqlParameter("@GenderId", employee.GenderId)
, new SqlParameter("@Photo", SqlDbType.Image) {Value= sqlhelper.ToDbValue(employee.Photo) }//需要指定专门image的类型,数据库不认识,
, new SqlParameter("@Id", employee.Id)
);
}
/// <summary>
/// 综合查询
/// </summary>
/// <param name="sql"></param>
/// <param name="parameter"></param>
/// <returns></returns>
public EmployeeList[] SearchBySome(string sql, List<SqlParameter> parameter)
{
DataTable table= sqlhelper.datatable(sql, parameter.ToArray());
EmployeeList[] items = new EmployeeList[table.Rows.Count];
for (int i = ; i < table.Rows.Count; i++)
{
items[i] = ToEmployeeListModel(table.Rows[i]);
} return items;
} } }
四:datetime.now和datetime.today区别
datetime.now 表示现在的最准确的时间,精确到秒
datetime.today 表示当天的时间,精确到天
综合查询员工和datetime.now和datetime.today区别的更多相关文章
- allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[System.DateTime]
allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[S ...
- Python datetime模块的datetime类
datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...
- python中datetime模块中datetime对象的使用方法
本文只讲述datetime模块中datetime对象的一些常用的方法,如果读者需要更多datetime模块的信息,请查阅此文档. datetime模块的对象有如下: timedelta date da ...
- [转] datetime.now、datetime.utcnow以及Django中的timezone.now之间的区别
https://blog.csdn.net/sinat_41898105/article/details/80984298 在我们使用Django做项目时经常会遇到时间的存储与读取不一致的问题 ...
- datetime与smalldatetime之间的区别
1.一直以为smalldatetime和datetime的差别只是在于时间范围: smalldatetime的有效时间范围1900/1/1~2079/6/6datetime的有效时间范围1753/1/ ...
- mysql建库DATETIME、DATE 和 TIMESTAMP区别
http://blog.sina.com.cn/s/blog_5f7b8c2c0100tf6t.html DATETIME.DATE 和 TIMESTAMP 类型是相似的.本篇描述了它们的特性 ...
- mysql中时间类型datetime,timestamp与int的区别
在mysql中存储时间,我们可以用datetime 格式,timestamp格式,也可以用int格式.那么我们设计的时候该如何考虑呢? 首先,我觉得应该明白这几个格式究竟是如何的,然后看看他们的区别, ...
- 在转换为 UTC 时大于 DateTime.MaxValue 或小于 DateTime.MinValue 的 DateTime 值无法系列化为 JSON
看到这个错往往会感到莫名其妙! 其原因其实是数据库的DateTime最小值和C#的DateTime不一样 Sql Server DateTime 类型必须介于 1/1/1753 12:00:00 AM ...
- DateTime.Now与DateTime.Today的区别
区别如下图: DateTime.Now: 不仅显示日期 还显示当前时间: DateTime.Today: 只显示当前日期,没有时间
随机推荐
- WisDom.Net 框架设计(二) 服务总线
WisDom.Net 框架设计--服务总线 1.Soa 简介 soa 就是面向服务的体系结构 是一个组件模型,不同的组件之间通过定义良好的接口联系起来.就像盖房子一块砖头一块砖头的砌墙,一片一 ...
- object标签参考(转载)
<object> 元素可支持多种不同的媒介类型,比如: 图片 音频 视频 Other 对象 显示图片 你可以显示一幅图片: <object height="100%&quo ...
- android入门系列- TextView EditText Button ImageView 的简单应用
第一篇原创,其实自己就是一菜鸟,简单分享点基本知识吧.希望能有所帮助吧. TextView EditText Button ImageView 这几个控件可能是Android开发中最常用.最基本的几个 ...
- angularjs hover
<ul class="pdl-15"><li ng-repeat="order in vm.selectOrders" ng-class=&q ...
- ajax xmlhttp下open方法POST、GET参数的区别
1. get是从服务器上获取数据(会暴露客户端ip),post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看 ...
- memcached和mongodb 在windows下安装
要在新机器上安装memcached和mongodb服务,折腾了一天,终于把这两个服务在windows下跑起来了. memcached主要参考http://www.rootop.org/pages/27 ...
- 在treeview外加一个滚动条的实现
前台代码: <div style="overflow:auto;width:190px;height:280px;border:1px solid #336699;padding-le ...
- JSP标准标签库的安装以及自定义标签的创建
JSTL 库安装 Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载 ...
- yii2源码学习笔记(四)
继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...
- PHP 关于MongoDB的操作
<?php header("Content-type:text/html;charset=utf-8"); $m = new MongoClient(); // 连接 $db ...