1、登录功能
登陆成功界面
登陆成功界面
关键代码
// 点击“登录”按钮则登录系统
private void bt_Login_Click(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令发送给数据库
String sqlStr = "select * from EMPLOYEE where ID=@id and PASSWORD=@pwd";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 注意是用用户ID登录,而不是用户名,用户名可能会重复
cmd.Parameters.Add(new SqlParameter("@id", this.tb_User.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@pwd", this.tb_Password.Text.Trim()));
SqlDataReader dr = cmd.ExecuteReader();
// 如果从数据库中查询到记录,则表示可以登录
if (dr.HasRows)
{
dr.Read();
UserInfo.userId = int.Parse(dr["id"].ToString());
UserInfo.userName = dr["name"].ToString();
UserInfo.userPwd = dr["password"].ToString();
UserInfo.userQx = dr["role"].ToString();
UserInfo.userAge = dr["gender"].ToString();
MessageBox.Show(UserInfo.userQx + "登录成功");
if (UserInfo.userQx == "管理员")
{
// 显示库管员主界面
MainFormAdmin formAdmin = new MainFormAdmin();
formAdmin.Show();
// 隐藏登录界面
this.Hide();
}
if (UserInfo.userQx == "职员")
{
// 显示收银员主界面
MainFormUser formUser = new MainFormUser();
formUser.Show();
// 隐藏登录界面
this.Hide();
}
}
else
{
MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
2、注册功能
注册界面
注册界面
编号自动生成代码
private void NewUserForm_Load(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
String sqlStr = "select MAX(id+1) as id from EMPLOYEE";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
this.id.Text = dr[0].ToString();
// 如果从数据库中查询到记录,则表示可以登录
}
关键代码
private void zc_Click(object sender, EventArgs e)
{
// 点击“确认”按钮,则录入信息
String id = this.id.Text.Trim();
String name = this.name.Text.Trim();
String pwd = this.pwd.Text.Trim();
String age = this.age.Text.Trim();
String sex = this.sex.Text.Trim();
String bm = this.bm.Text.Trim();
String qx = this.qx.Text.Trim();
/*int supplier = int.Parse(this.cbb_Supplier.SelectedValue.ToString());*/
// 更新数据库
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "insert into EMPLOYEE(ID, NAME, PASSWORD, AGE, QX, SEX, BM) values(@id, @name, @pwd, @age, @qx, @sex, @bm )";
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("@age", age));
cmd.Parameters.Add(new SqlParameter("@qx", qx));
cmd.Parameters.Add(new SqlParameter("@sex", sex));
cmd.Parameters.Add(new SqlParameter("@bm", bm));
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否插入成功
if (res != 0)
{
MessageBox.Show("员工信息录入成功");
}
else
{
MessageBox.Show("员工信息录入失败");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
网友评论