Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            AC.openConnection();

            if (string.IsNullOrEmpty(textBoxEID.Text.Trim()) || string.IsNullOrEmpty(textBoxEpass.Text.Trim()))
            {
                MessageBox.Show("社員IDとパスワードを入力してください", "データ入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (textBoxEID.CanSelect)
                {
                    textBoxEID.Select();
                }

                textBoxEID.SelectAll();
                textBoxEpass.Text = string.Empty;

                return;
            }

            AC.sql = "select 社員ID, パスワード, 社員名, ステータス from 社員マスタ where 社員ID = @id and パスワード = @pa;";
            AC.cmd.Parameters.Clear();
            AC.cmd.CommandType = CommandType.Text;
            AC.cmd.CommandText = AC.sql;

            AC.cmd.Parameters.AddWithValue("@id", textBoxEID.Text.Trim());
            AC.cmd.Parameters.AddWithValue("@pa", Sha256hash.ToHash(textBoxEpass.Text.Trim()));

            AC.rd = AC.cmd.ExecuteReader();

            if (AC.rd.HasRows)
            {
                while (AC.rd.Read())
                {
                    AC.currentFullName = AC.rd[2].ToString();
                    status             = int.Parse(AC.rd[3].ToString());
                    MessageBox.Show("ようこそ " + AC.currentFullName + "さん", "ログイン成功", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }

                AC.currentID = int.Parse(textBoxEID.Text);

                textBoxEID.Text   = string.Empty;
                textBoxEpass.Text = string.Empty;

                Hide();

                Form menuForm = new MainMenuForm();
                menuForm.Show();
            }
            else if (status != 0)
            {
                MessageBox.Show("社員IDかパスワードが違います。もう一度やり直してください。", "データ入力エラー", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                if (textBoxEID.CanSelect)
                {
                    textBoxEID.Select();
                }
            }
            else
            {
                MessageBox.Show("社員IDかパスワードが違います。もう一度やり直してください。", "データ入力エラー", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                if (textBoxEID.CanSelect)
                {
                    textBoxEID.Select();
                }
            }

            textBoxEID.SelectAll();
            textBoxEpass.Text = string.Empty;

            AC.rd.Close();
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (dataGridViewElist.SelectedRows.Count <= 0 || dataGridViewElist.CurrentRow.Cells[0].Value.ToString() == "")
            {
                if (string.IsNullOrEmpty(textBoxEname.Text.Trim()) || string.IsNullOrEmpty(textBoxEhurigana.Text.Trim()) ||
                    string.IsNullOrEmpty(textBoxEpost.Text.Trim()) || string.IsNullOrEmpty(textBoxEaddress.Text.Trim()) ||
                    string.IsNullOrEmpty(textBoxEphone.Text.Trim()) || string.IsNullOrEmpty(textBoxEmail.Text.Trim()) ||
                    string.IsNullOrEmpty(comboBoxEsex.Text.Trim()) || string.IsNullOrEmpty(dateTimePickerEbirth.Text.Trim()) ||
                    string.IsNullOrEmpty(textBoxEpass.Text.Trim()))
                {
                    MessageBox.Show("全てのデータ項目を入力してください", "データ入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    if (textBoxEmail.Text.IndexOf('@') == -1)
                    {
                        MessageBox.Show("これは有効なメールアドレスではありません", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEmail.Focus();
                        textBoxEmail.SelectAll();
                        return;
                    }

                    var index = textBoxEmail.Text.IndexOf('@');
                    if (textBoxEmail.Text.IndexOf('@', index + 1) != -1)
                    {
                        MessageBox.Show("これは有効なメールアドレスではありません", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEmail.Focus();
                        textBoxEmail.SelectAll();
                        return;
                    }

                    var iLengthpost  = textBoxEpost.TextLength;
                    var iLengthphone = textBoxEphone.TextLength;

                    if (iLengthpost < 7)
                    {
                        MessageBox.Show("正しい郵便番号を入力してください", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEpost.Focus();
                        textBoxEpost.SelectAll();
                        return;
                    }

                    if (iLengthphone < 10)
                    {
                        MessageBox.Show("正しい電話番号を入力してください", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEphone.Focus();
                        textBoxEphone.SelectAll();
                        return;
                    }

                    var iLength = textBoxEpass.TextLength;
                    var passwd  = textBoxEpass.Text;

                    if (iLength < 8)
                    {
                        MessageBox.Show("パスワードが短すぎます。8文字以上で入力してください", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEpass.Focus();
                        textBoxEpass.SelectAll();
                        return;
                    }

                    if (iLength >= 8)
                    {
                        var answer = 0;
                        foreach (var c in passwd)
                        {
                            if (answer == 1)
                            {
                                break;
                            }
                            if (!char.IsUpper(c))
                            {
                            }
                            else
                            {
                                answer = 1;
                            }
                        }

                        if (answer == 0)
                        {
                            MessageBox.Show("パスワードに大文字が含まれていません。", "データ入力エラー", MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                            textBoxEpass.Focus();
                            textBoxEpass.SelectAll();
                            return;
                        }
                    }

                    try
                    {
                        var msg     = "データを追加しますか?";
                        var caption = "データの追加";
                        var buttons = MessageBoxButtons.YesNo;
                        var ico     = MessageBoxIcon.Question;

                        DialogResult result;

                        result = MessageBox.Show(this, msg, caption, buttons, ico);

                        if (result == DialogResult.Yes)
                        {
                            AC.sql =
                                "insert into 社員マスタ(社員名, ふりがな, 性別, 生年月日, 郵便番号, 住所, 電話番号, メールアドレス, 入社日, パスワード, ステータス) Values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                            AC.cmd.Parameters.Clear();
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEname.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEhurigana.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = comboBoxEsex.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.Date).Value     = dateTimePickerEbirth.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEpost.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEaddress.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEphone.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEmail.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.Date).Value     = dateTimePickerEjoin.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = Sha256hash.ToHash(textBoxEpass.Text);
                            AC.cmd.Parameters.Add("?", OleDbType.Integer).Value  = 0;

                            AC.cmd.CommandText = AC.sql;
                            var rows = AC.cmd.ExecuteNonQuery();
                            if (rows >= 1)
                            {
                                RefreshLoad();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("データの追加に失敗しました: " + ex.Message, "データの追加", MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                if (string.IsNullOrEmpty(textBoxEname.Text.Trim()) || string.IsNullOrEmpty(textBoxEhurigana.Text.Trim()) ||
                    string.IsNullOrEmpty(textBoxEpost.Text.Trim()) || string.IsNullOrEmpty(textBoxEaddress.Text.Trim()) ||
                    string.IsNullOrEmpty(textBoxEphone.Text.Trim()) || string.IsNullOrEmpty(textBoxEmail.Text.Trim()) ||
                    string.IsNullOrEmpty(comboBoxEsex.Text.Trim()) || string.IsNullOrEmpty(dateTimePickerEbirth.Text.Trim()) ||
                    string.IsNullOrEmpty(textBoxEpass.Text.Trim()) || string.IsNullOrEmpty(textBoxEpass.Text.Trim()))
                {
                    MessageBox.Show("全てのデータ項目を入力してください", "データ入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    if (textBoxEmail.Text.IndexOf('@') == -1)
                    {
                        MessageBox.Show("これは有効なメールアドレスではありません", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEmail.Focus();
                        textBoxEmail.SelectAll();
                        return;
                    }

                    var index = textBoxEmail.Text.IndexOf('@');
                    if (textBoxEmail.Text.IndexOf('@', index + 1) != -1)
                    {
                        MessageBox.Show("これは有効なメールアドレスではありません", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEmail.Focus();
                        textBoxEmail.SelectAll();
                        return;
                    }

                    var iLengthpost  = textBoxEpost.TextLength;
                    var iLengthphone = textBoxEphone.TextLength;

                    if (iLengthpost < 7)
                    {
                        MessageBox.Show("正しい郵便番号を入力してください", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEpost.Focus();
                        textBoxEpost.SelectAll();
                        return;
                    }

                    if (iLengthphone < 10)
                    {
                        MessageBox.Show("正しい電話番号を入力してください", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEphone.Focus();
                        textBoxEphone.SelectAll();
                        return;
                    }


                    var iLength = textBoxEpass.TextLength;
                    var passwd  = textBoxEpass.Text;

                    if (iLength < 8)
                    {
                        MessageBox.Show("パスワードが短すぎます。8文字以上で入力してください", "データ入力エラー", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        textBoxEpass.Focus();
                        textBoxEpass.SelectAll();
                        return;
                    }

                    if (iLength >= 8)
                    {
                        var answer = 0;
                        foreach (var c in passwd)
                        {
                            if (answer == 1)
                            {
                                break;
                            }
                            if (!char.IsUpper(c))
                            {
                            }
                            else
                            {
                                answer = 1;
                            }
                        }

                        if (answer == 0)
                        {
                            MessageBox.Show("パスワードに大文字が含まれていません。", "データ入力エラー", MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                            textBoxEpass.Focus();
                            textBoxEpass.SelectAll();
                            return;
                        }
                    }

                    try
                    {
                        var msg     = "データの編集を反映しますか?";
                        var caption = "データの編集";
                        var buttons = MessageBoxButtons.YesNo;
                        var ico     = MessageBoxIcon.Question;

                        DialogResult result;

                        result = MessageBox.Show(this, msg, caption, buttons, ico);

                        if (result == DialogResult.Yes)
                        {
                            var id = int.Parse(dataGridViewElist.CurrentRow.Cells[0].Value.ToString());
                            AC.sql =
                                "update 社員マスタ set 社員名 = ?, ふりがな = ?, 性別 = ?, 生年月日 = ?, 郵便番号 = ?, 住所 = ?, 電話番号 = ?, メールアドレス = ?, 入社日 = ? where 社員ID = @id;";
                            AC.cmd.Parameters.Clear();
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEname.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEhurigana.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = comboBoxEsex.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.Date).Value     = dateTimePickerEbirth.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEpost.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEaddress.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEphone.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.VarWChar).Value = textBoxEmail.Text;
                            AC.cmd.Parameters.Add("?", OleDbType.Date).Value     = dateTimePickerEjoin.Text;
                            AC.cmd.Parameters.Add("@id", OleDbType.BigInt).Value = id;

                            AC.cmd.CommandText = AC.sql;
                            var rows = AC.cmd.ExecuteNonQuery();
                            if (rows >= 1)
                            {
                                RefreshLoad();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("データの編集に失敗しました: " + ex.Message, "データの編集", MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                }
            }
        }