private void OkButton_Click(object sender, RoutedEventArgs e) { if (AccountTextBox.Text == null || PassWord.Password == null) { System.Windows.MessageBox.Show("请填写账号和密码。", "信息未填写", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); return; } else if (AccountTextBox.Text.Length > 25) { System.Windows.MessageBox.Show("账号名称过长,最大为25位。", "填写错误", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); return; } string connectionStr = "Data Source=" + DatabaseInfo.dataSource + ";UID=" + DatabaseInfo.uid + ";PWD=" + DatabaseInfo.pwd + ";Initial Catalog=AccountDB;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionStr)) { string queryString = "SELECT * FROM dbo.Account WHERE Name = " + AccountTextBox.Text; SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { string passwordMD5; using (MD5 md5Hash = MD5.Create()) { passwordMD5 = CalcMD5.GetMd5Hash(md5Hash, PassWord.Password); } if ((string)reader[1] == passwordMD5) { System.Windows.MessageBox.Show("登录成功。", "成功", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); reader.Close(); MainWindow mainWindow = new MainWindow(); mainWindow.Show(); this.Close(); } else { System.Windows.MessageBox.Show("密码错误,请检查。", "错误", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); reader.Close(); return; } } else { System.Windows.MessageBox.Show("账号不存在,请检查。", "错误", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); reader.Close(); } } }
private void OkButton_Click(object sender, RoutedEventArgs e) { if (AccountTextBox.Text == null || PassWord.Password == null) { System.Windows.MessageBox.Show("请填写账号和密码。", "信息未填写", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); return; } else if (AccountTextBox.Text.Length > 25) { System.Windows.MessageBox.Show("账号名称过长,最大为25位。", "填写错误", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); return; } string connectionStr = "Data Source=" + DatabaseInfo.dataSource + ";UID=" + DatabaseInfo.uid + ";PWD=" + DatabaseInfo.pwd + ";Initial Catalog=AccountDB;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionStr)) { string queryString = "SELECT * FROM dbo.Account WHERE Name = " + AccountTextBox.Text; SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { System.Windows.MessageBox.Show("账号已注册,您可以直接登录。", "账号已注册", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); reader.Close(); return; } else { reader.Close(); string passwordMD5; using (MD5 md5Hash = MD5.Create()) { passwordMD5 = CalcMD5.GetMd5Hash(md5Hash, PassWord.Password); } string queryStringInsert = "insert into dbo.Account (Name, Password) values ('" + AccountTextBox.Text + "', '" + passwordMD5 + "')"; SqlCommand commandInsert = new SqlCommand(queryStringInsert, connection); commandInsert.ExecuteNonQuery(); MessageBox.Show("注册成功。", "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); this.Close(); } } }