private void customer_login3_Click(object sender, EventArgs e) { String username = this.customer_login1.Text; String password = this.customer_login2.Text; Database1 db1 = new Database1(); MySqlConnection conn1 = db1.getConn(); if (username.Equals("") || password.Equals("")) { MessageBox.Show("账号或密码为空"); } else { try { conn1.Open(); String sql1 = String.Format("select * from user_customer where username= '******' ", username); MySqlCommand comm2 = new MySqlCommand(sql1, conn1); if (Convert.ToInt32(comm2.ExecuteScalar()) > 0) { String sql = String.Format("select username,password from user_customer where username = '******' limit 1", username); MySqlCommand comm1 = new MySqlCommand(sql, conn1); MySqlDataReader reader = comm1.ExecuteReader(); while (reader.Read()) { String username1 = reader[0].ToString(); String password1 = reader[1].ToString(); if (username1 != null && password.Equals(password1)) { MessageBox.Show("登录成功"); } else { MessageBox.Show("密码错误,请重新输入"); } } reader.Close(); } else { MessageBox.Show("账户不存在,请注册"); } } catch (Exception exception) { Console.WriteLine(exception); MessageBox.Show(exception.Message.ToString()); } finally { conn1.Close(); } } }
private void register_customer_Click(object sender, EventArgs e) { String username = this.register_customer1.Text; String password = this.register_customer2.Text; Database1 db1 = new Database1(); MySqlConnection conn1 = db1.getConn(); if (username.Equals("") || password.Equals("")) { MessageBox.Show("账号密码为空"); } else { try { conn1.Open(); String sql1 = String.Format("select * from user_customer where username= '******'", username); MySqlCommand comm1 = new MySqlCommand(sql1, conn1); String sql2 = String.Format("insert into user_customer (username,password) values ('{0}','{1}')", username, password); MySqlCommand comm2 = new MySqlCommand(sql2, conn1); if (Convert.ToInt32(comm1.ExecuteScalar()) > 0) { MessageBox.Show("账号已经注册,请登录"); } else { comm2.ExecuteNonQuery(); MessageBox.Show("注册成功"); } } catch (Exception exception) { Console.WriteLine(exception); throw; } finally { conn1.Close(); } } }