1尘应、打卡
1)效果圖
捕獲.PNG
2)畫面實現(xiàn)效果
可以打開串口惶凝,進行卡片信息寫入并讀取卡號實現(xiàn)打卡功能。
3)重要代碼片段及詳細描述
1犬钢、尋卡苍鲜,將RFID的卡號讀出來
string response = "";
serialPort1.Write(ISO15693Card.COMMAND_WRITE_REG);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_SET_AGC);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_SET_RECV_MODE);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_INVEN_CARD);// 尋卡
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
List<ISO15693Card> cards = ISO15693CardHandler.InventoryCard(response);
2、讀取卡上第00塊的數據玷犹,獲得職員編號
String stuffId = ReadSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "00");
3混滔、向數據庫中插入一條打卡記錄,插入成功后顯示打卡成功
String connStr = ConfigurationManager.ConnectionStrings["Attendance system"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數據庫
sqlConn.Open();
// 構造命令
String sqlStr = "insert into record(employee_id, date, time, machine_id) VALUES(@Employee_id, @date,@time, @machine_id)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數賦值
cmd.Parameters.Add(new SqlParameter("@employee_id", stuffId));
cmd.Parameters.Add(new SqlParameter("@date", DateTime.Now.ToString("yyyy - MM - dd").ToString()));
cmd.Parameters.Add(new SqlParameter("@time", DateTime.Now.ToString("HH: mm:ss").ToString()));
cmd.Parameters.Add(new SqlParameter("@machine_id", '1'));
// 將命令發(fā)送給數據庫
int res = cmd.ExecuteNonQuery();
// 根據返回值判斷是否插入成功
if (res != 0)
{
MessageBox.Show("打卡成功");
}
else
{
MessageBox.Show("打卡失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數據庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
2歹颓、查詢
1)效果圖
員工查詢.gif
2)畫面實現(xiàn)效果
員工登錄系統(tǒng)后可查詢自己的歷史打卡記錄坯屿;
管理員登錄系統(tǒng)后可查詢所有員工的打卡記錄。
3)重要代碼片段及詳細描述
1巍扛、窗口加載時领跛,顯示當前時間,顯示當前用戶名
private void MainForm_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數據加載到表“AttendanceDataSet.record”中电湘。您可以根據需要移動或刪除它隔节。
this.recordTableAdapter.Fill(this.AttendanceDataSet.record);
this.tssl_CurrentTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
this.tssl_User.Text = UserInfo.userQx + UserInfo.userName;
}
2、員工查詢打卡信息
private void tsmi_Search_Click(object sender, EventArgs e)
{
RecordForm recordForm = new RecordForm();
recordForm.MdiParent = this;
recordForm.WindowState = FormWindowState.Maximized;
recordForm.Show();
}
3寂呛、工作時長計算
String sqlStr = @"select t4.department, t4.name, t3.date, t3.start_time, t3.end_time, t3.diff from (
select t1.employee_id, t1.date, t1.time as start_time, t2.time as end_time, datediff(n,t1.time,t2.time) as diff
from record t1
inner join record t2
on t1.date = t2.date
and t1.employee_id=t2.employee_id
and t1.machine_id=1
and t2.machine_id=2
and t1.date>=@begin
and t1.date<=@end
) t3, employee t4 where t3.employee_id=t4.id and t3.diff<540 and t4.department=@department";
3怎诫、統(tǒng)計
1)效果圖
統(tǒng)計.PNG
詳情.PNG
2)畫面實現(xiàn)效果
管理員登錄系統(tǒng),可以查看統(tǒng)計部門考勤情況
3)重要代碼片段及詳細描述
1贷痪、查看部門考勤
private void 統(tǒng)計部門考勤DToolStripMenuItem_Click(object sender, EventArgs e)
{
BmRecordForm bmrecordForm = new BmRecordForm();
bmrecordForm.MdiParent = this;
bmrecordForm.WindowState = FormWindowState.Maximized;
bmrecordForm.Show();
}
2幻妓、連接到數據庫
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
sqlConn.Open();
{
String sqlStr = @"select t4.department, t4.name, t3.date, t3.start_time, t3.end_time, t3.diff from (
select t1.employee_id, t1.date, t1.time as start_time, t2.time as end_time, datediff(n,t1.time,t2.time) as diff
from record t1
inner join record t2
on t1.date = t2.date
and t1.employee_id=t2.employee_id
and t1.machine_id=1
and t2.machine_id=2
and t1.date>=@begin
and t1.date<=@end
) t3, employee t4 where t3.employee_id=t4.id and t3.diff<540 and t4.department=@department";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
cmd.Parameters.Add(new SqlParameter("@begin", this.begin.ToShortDateString()));
cmd.Parameters.Add(new SqlParameter("@end", this.end.ToShortDateString()));
cmd.Parameters.Add(new SqlParameter("@department", this.department));
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
3、綁定數據表
DataSet ds = new DataSet();
// 自定義一個表(MyGoods)來標識數據庫的GOODS表
adp.Fill(ds, "Attendance");
// 指定DataGridView的數據源為DataSet的MyGoods表
this.dataGridView1.DataSource = ds.Tables["Attendance"];
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數據庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}