private void button1_Click(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(connstr)) { string sql = "select Password,UserType,ID from StaffAccount where Name='" + textBox1.Text + "'"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { conn.Open();//打开连接 using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { string pwd = reader.GetString(0).Trim(); string utype = reader.GetString(1); int id = reader.GetInt32(2); if (pwd == textBox2.Text) { UserName = textBox1.Text; UserType = utype; ID = id; MessageBox.Show("系统登录成功,正在跳转主页面..."); MainForm mainForm = new MainForm(); mainForm.Show(); this.Hide(); } else { MessageBox.Show("密码错误!请再次输入!"); textBox2.Text = ""; } } else { MessageBox.Show("用户名不存在,请重新出入!"); textBox1.Text = ""; } } } } }
private void Change_Click(object sender, EventArgs e) { string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication6.Properties.Settings.staffConnectionString1"].ConnectionString; SqlConnection conn = new SqlConnection(connstr); string sql = "select Password from StaffAccount where Name ='" + txtName.Text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read()) { string oldPwd = sdr.GetString(0).Trim(); if (oldPwd == txtOldPwd.Text) { if (txtNewPassword.Text.Trim() == "" || txtNewPwdAgain.Text.Trim() == "") { MessageBox.Show("新密码确认不能为空!"); return; } else if (txtNewPassword.Text.Trim() != txtNewPwdAgain.Text.Trim()) { MessageBox.Show("2次输入的新密码不一样,请重新输入!"); txtNewPassword.Text = ""; txtNewPwdAgain.Text = ""; return; } else { sdr.Close(); string sqlUpdate = "update StaffAccount set Password ='******' where Name ='" + txtName.Text + "'"; SqlCommand cmdUp = new SqlCommand(sqlUpdate, conn); if (cmdUp.ExecuteNonQuery() == 0) { MessageBox.Show("未知错误!"); return; } else { MainForm mainForm = new MainForm(); mainForm.Show(); this.Close(); MessageBox.Show("恭喜你!密码修改成功!"); } } } else { MessageBox.Show("旧密码错误或者不能为空"); txtOldPwd.Text = ""; txtNewPassword.Text = ""; txtNewPwdAgain.Text = ""; return; } } else { MessageBox.Show("用户名不存在,请重新输入!"); txtName.Text = ""; txtOldPwd.Text = ""; txtNewPassword.Text = ""; txtNewPwdAgain.Text = ""; return; } conn.Close(); }
private void button1_Click(object sender, EventArgs e) { string sql = "select Name from StaffAccount where Name='" + txtUserName + "'"; string connstr = ConfigurationManager.ConnectionStrings["WindowsFormsApplication6.Properties.Settings.staffConnectionString1"].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(); } }