一、任務(wù)需求
-
管理員可查詢?nèi)咳藛T的打卡記錄,同時(shí)也可附加條件查詢特定人員記錄
-
普通職員僅可查詢自己的打卡記錄
二、準(zhǔn)備(查詢界面)

查詢界面.jpg
三、管理員查詢(主要代碼)
private void bt_Query_Click(object sender, EventArgs e)
{
// 連接字符串,注意與實(shí)際環(huán)境保持一致
String connStr1 = ConfigurationManager.ConnectionStrings["KQ"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr1);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
// 構(gòu)造命令(查詢所有)
String sqlStr = "select * from record";
// 添加查詢條件
if (!this.tb_Id.Text.Trim().Equals(""))
{
sqlStr += " where employee_id='" + this.tb_Id.Text.Trim() + "'";
}
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 將該查詢過(guò)程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個(gè)表來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的record表
adp.Fill(ds, "cx1");
// 指定DataGridView的數(shù)據(jù)源為DataSet的cx表
this.dgv_Goods.DataSource = ds.Tables["cx1"];
}
catch (Exception exp)
{
MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
四、普通職員查詢(主要代碼)
private void bt_Query_Click(object sender, EventArgs e)
{
// 連接字符串,注意與實(shí)際環(huán)境保持一致
String connStr1 = ConfigurationManager.ConnectionStrings["KQ"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr1);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
// 構(gòu)造命令
String sqlStr = "select * from record";
// 添加查詢條件
if (!this.tb_Id.Text.Trim().Equals(""))
{
sqlStr += " where employee_id='" + this.tb_Id.Text.Trim() + "'";
}
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 將該查詢過(guò)程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個(gè)表來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)的record表
adp.Fill(ds, "cx1");
// 指定DataGridView的數(shù)據(jù)源為DataSet的cx表
this.dgv_Goods.DataSource = ds.Tables["cx1"];
}
catch (Exception exp)
{
MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
五、成果展示
1、管理員查詢

管理員查詢.gif
2、普通員工514查詢

普通員工查詢.gif
3、登錄表數(shù)據(jù)

登陸數(shù)據(jù)庫(kù).png
4、考勤數(shù)據(jù)表

考勤數(shù)據(jù)表.gif