// 确定按钮 private void Ok_Click(object sender, EventArgs e) { string username = TextAccount.Text.Trim(); string password = TextPasswd.Text.Trim(); string sex = TextSex.Text.Trim(); string birthday = TextBirthday.Text.Trim(); string telephone = TextPhone.Text.Trim(); string email = TextEmail.Text.Trim(); String2NULL(ref username); String2NULL(ref password); String2NULL(ref sex); String2NULL(ref birthday); String2NULL(ref telephone); String2NULL(ref email); #region 查询数据库当前账号是否被注册过 sqlCon.Open(); string sql_cmd = String.Format("select * from [user] where username={0}", username); SqlDataAdapter sda = new SqlDataAdapter(sql_cmd, sqlCon); DataSet ds = new DataSet(); sda.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { MessageBox.Show("当前账户名已被使用,请重新输入!"); } else { sql_cmd = String.Format("insert into [user] values({0},{1},{2},{3},{4},{5},0,0,NULL,NULL)", username, password, sex, birthday, telephone, email); SqlCommand SqlCmd = new SqlCommand(sql_cmd, sqlCon); SqlCmd.ExecuteNonQuery(); MessageBox.Show("账号创建成功!"); } TextAccount.Clear(); TextPasswd.Clear(); TextSex.Clear(); TextBirthday.Clear(); TextPhone.Clear(); TextEmail.Clear(); TextAccount.Focus(); sqlCon.Close(); #endregion }
// 登陆按钮 private void Ok_Click(object sender, EventArgs e) { if (error_cnt == 3) { MessageBox.Show("账号或密码错误次数已达3次,无法登陆!"); TextAccount.Clear(); TextPasswd.Clear(); TextAccount.Focus(); return; } string username = TextAccount.Text.Trim(); string password = TextPasswd.Text.Trim(); if (username == "" || password == "") { MessageBox.Show("账户或密码为空,请重新输入!"); TextAccount.Clear(); TextPasswd.Clear(); TextAccount.Focus(); return; } #region 检测账号密码是否正确 sqlCon.Open(); string sql_cmd = String.Format(@"select * from [user] where username='******' and passwd='{1}'" , username, password); SqlCommand SqlCmd = new SqlCommand(sql_cmd, sqlCon); SqlDataReader sdr = SqlCmd.ExecuteReader(); if (sdr.HasRows) { MessageBox.Show("登陆成功!"); error_cnt = 0; this.Hide(); sdr.Read(); if (sdr["school_name"] == DBNull.Value) { new ChooseSchoolMajor(this, username).Show(); } else { new HomePage(this, this.Location, username, (string)sdr["school_name"], (string)sdr["major_name"]).Show(); } } else { error_cnt++; MessageBox.Show("用户名或密码错误,请重新输入,您还有" + Convert.ToString(3 - error_cnt) + "次机会!"); TextAccount.Clear(); TextPasswd.Clear(); TextAccount.Focus(); } sdr.Close(); sqlCon.Close(); #endregion }