登录及注册
登录界面
登录界面代码
调用数据库识别用户
if (dr.HasRows)
{
dr.Read();
UserInfo.userId = int.Parse(dr["ID"].ToString());
UserInfo.userName = dr["NAME"].ToString();
UserInfo.userPwd = dr["PASSWORD"].ToString();
UserInfo.userRole = dr["ROLE"].ToString();
UserInfo.userDepartment = dr["DEPARTMENT"].ToString();
UserInfo.userGender = dr["GENDER"].ToString();
MessageBox.Show(UserInfo.userRole + "登录成功");
if (UserInfo.userRole == "职员")
{
}
if (UserInfo.userRole == "管理员")
{
MainFormAdmin formAdmin = new MainFormAdmin();
formAdmin.Show();
this.Hide();
}
登录成功后跳转到管理员界面
private void ll_Register_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
RecordForm form = new RecordForm();
form.Show();
}
管理员界面
管理员界面代码
设置父子窗口
private void tsmi_Record_Click(object sender, EventArgs e)
{
/*
RecordForm recordForm = new RecordForm();
recordForm.MdiParent = this;
recordForm.Show();
*/
//RecordForm2 recordForm = new RecordForm2();
//recordForm.MdiParent = this;
//recordForm.Show();
RecordForm form = new RecordForm();
form.MdiParent = this;
form.Show();
}
注册界面
注册界面代码
调用参数
private void bt_Ok_Click(object sender, EventArgs e)
{
String id = this.tb_Id.Text.Trim();
String name = this.tb_Name.Text.Trim();
String pwd = this.tb_pwd.Text.Trim();
String gender = this.comboBox1.Text.Trim();
String department = this.comboBox2.Text.Trim();
String role = this.comboBox3.Text.Trim();
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "insert into EMPLOYEE(ID, NAME, PASSWORD, GENDER, DEPARTMENT,ROLE) values(@id, @name, @pwd, @gender, @department, @role)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("@pwd", pwd));
cmd.Parameters.Add(new SqlParameter("@gender", gender));
cmd.Parameters.Add(new SqlParameter("@department", department));
cmd.Parameters.Add(new SqlParameter("@role", role));
}
在comboBox中调用数据库信息
private void RecordForm_Load(object sender, EventArgs e)
{
comboBox3.SelectedIndex = 0;
comboBox1.SelectedIndex = 0;
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
sqlConn.Open();
String sqlStr1 = "select MAX(id+1) as id from employee";
String sqlStr2 = "select distinct(department) from employee";
SqlCommand cmd1 = new SqlCommand(sqlStr1, sqlConn);
SqlCommand cmd2 = new SqlCommand(sqlStr2, sqlConn);
SqlDataReader xh = cmd1.ExecuteReader();
if (xh.HasRows)
{
xh.Read();
tb_Id.Text = xh["ID"].ToString();
}
sqlConn.Close();
sqlConn.Open();
SqlDataReader dr = cmd2.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
comboBox2.Items.Add(dr["DEPARTMENT"].ToString());
}
comboBox2.SelectedIndex = 0;
sqlConn.Close();
}
查询界面代码
、、、
private void button1_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
//2.利用DataAdapter对象,建立与数据库的连接桥
string str_1 = dateTimePicker1.Value.ToString();
string str_2 = dateTimePicker2.Value.ToString();
String sqlStr = "select serial,date,time,machine_id,employee_id from record where date>=@str_1 and date<=@str_2 and employee_id = @id; ";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));
cmd.Parameters.Add(new SqlParameter("@str_1", str_1));
cmd.Parameters.Add(new SqlParameter("@str_2", str_2));
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.SelectCommand = cmd;
//3.通过DataAdapter桥,将查询结果存储到DataSet对象中
DataSet ds = new DataSet();
adapter.Fill(ds);
//4.利用DataGridView控件将DataSet中的查询结果显示出来
dataGridView1.DataSource = ds.Tables[0];
//5.关闭数据库连接
conn.Close();
}
职员界面代码
private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
q aaForm = new q();
//pwdForm.MdiParent = this;
//pwdForm.StartPosition = FormStartPosition.CenterScreen;
aaForm.Show();
}