上一章已经完成了删除用户的功能,现在来完成用户注册功能。
因为是员工管理系统,也就是公司内部使用的,所以应该由公司的管理员为新员工注册账号。
在这之前先创建一个MainForm窗体用来管理系统的所以功能
将工具栏中ToolStrip控件拖入新建的MainForm窗体,添加五个按钮如图:
更改Form1中跳转程序
MessageBox.Show("系统登录成功,正在跳转主页面...");
MainForm mainForm = new MainForm();
mainForm.Show();
this.Hide();
双击“管理界面”添加
ManagerForm managerForm = new ManagerForm();
managerForm.Show();
双击“退出系统”添加
Application.Exit();
包含控件Lable,TextBox,Button,GroupBox,RadioButton,ComboBox
注意:前四个TextBox控件后Lable控件,用来提示用户输入了错误信息
控件Name属性(txtUserName,txtPassword,txtPwdConfirm,txtName,txtAge,cmboxSex,cmboxOffice,rbtNormalUser,rbtAdmin)
编辑cmboxSex,cmboxOffice如图:
这里我们希望ComboBox不可编辑,这样可以避免用户输入一些错误的值造成程序崩溃。解决的方法是将其DropDownStyle属性设置为DropDownList。
双击“注册”按钮,添加事件代码
注意:因为StaffInfo.ID为主键,StaffAccount.ID为外键,所有应先将记录添加到StaffInfo表中,再添加到StaffAccount表中
private void button1_Click(object sender, EventArgs e)
{
string sql = "select Name from StaffAccount where Name='" + txtUserName + "'";
string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication6.Properties.Settings.staffConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
lblUserMsg.Text = "用户名已存在,请重新输入!";
}
else if (txtUserName.Text.Trim() == "")
{
lblUserMsg.Text = "用户名不能为空!";
}
else if (txtPassword.Text.Trim() == "")
{
lblPwd.Text = "密码不能为空!";
lblPwd.Text = "";
}
else if (txtPwdConfirm.Text.Trim() == "")
{
lblPswConfirm.Text = "验证密码不能为空!";
lblUserMsg.Text = "";
lblPwd.Text = "";
}
else if (txtPassword.Text.Trim() != txtPwdConfirm.Text.Trim())
{
lblPwd.Text = "2次密码必须一样!";
lblPswConfirm.Text = "请重新输入!";
}
else if (txtName.Text.Trim() == "" | txtAge.Text.Trim() == "" | cmboxSex.Text == "" | cmboxOffice.Text == "")
{
lblBaseInfo.Text = "基本信息不完整!";
}
else
{
lblUserMsg.Text = "";
lblPwd.Text = "";
lblPswConfirm.Text = "";
lblBaseInfo.Text = "";
string uType = "";
if (rbtAdmin.Checked)
uType = "Administrator";
else if (rbtNormalUser.Checked)
uType = "NormalUser";
else
uType = "NormalUser";
conn.Close();
string sqlInsert = "insert into StaffAccount(Name,Password,UserType) values(@UserName,@UserPwd,@UserType)";
string sqlInsertInfo = "insert into StaffInfo(Name,Sex,Age,Office) values(@Name,@Sex,@Age,@Office)";
SqlParameter[] param = {
new SqlParameter("@UserName",txtUserName.Text),
new SqlParameter("@UserPwd",txtPassword.Text),
new SqlParameter("@UserType",uType)
};
SqlParameter[] paramInfo = {
new SqlParameter("@Name",txtName.Text),
new SqlParameter("@Age",txtAge.Text),
new SqlParameter("@Sex",cmboxSex.Text),
new SqlParameter("@Office",cmboxOffice.Text)
};
SqlConnection connInsert = new SqlConnection(connstr);
SqlCommand cmdInsertInfo = new SqlCommand(sqlInsertInfo, connInsert);
SqlCommand cmdInsert = new SqlCommand(sqlInsert, connInsert);
connInsert.Open();
cmdInsertInfo.Parameters.AddRange(paramInfo);
cmdInsert.Parameters.AddRange(param);
int m = cmdInsertInfo.ExecuteNonQuery();
int n = cmdInsert.ExecuteNonQuery();
if (n == 0||m==0)
{
MessageBox.Show("注册失败!,请重新输入");
return;
}
else
{
MainForm mainForm = new MainForm();
mainForm.Show();
this.Close();
MessageBox.Show("注册成功!");
}
connInsert.Close();
}
}
双击“返回”按钮
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}