public bool ThaoTacVoiTaiKhoan(clsTaiKhoan_DTO TK, string command) { SqlConnection con = ThaoTacDuLieu_DAO.TaoKetNoi(); string query = ""; if (command == "Add") { query = "INSERT INTO TaiKhoan(ma_dang_nhap, mat_khau, ngay_tao, trang_thai)" + " VALUES(@ma_dang_nhap, @mat_khau, @ngay_tao, @trang_thai)"; } else if (command == "Update") { query = "UPDATE TaiKhoan SET mat_khau = @mat_khau, ngay_tao = @ngay_tao, trang_thai = @trang_thai WHERE ma_dang_nhap = @ma_dang_nhap"; } else if (command == "Delete") { query = string.Format("UPDATE TaiKhoan SET trang_thai = {0} WHERE ma_dang_nhap = '{1}'", TK.TrangThai, TK.MaDangNhap); } SqlCommand cmd = ThaoTacDuLieu_DAO.TruyVan(query, con); if (command == "Add" || command == "Update") { cmd.Parameters.AddWithValue("@ma_dang_nhap", TK.MaDangNhap); cmd.Parameters.AddWithValue("@mat_khau", TK.MatKhau); cmd.Parameters.AddWithValue("@ngay_tao", TK.NgayTao); //cmd.Parameters.AddWithValue("@ma_quyen_dang_nhap", TK.MaQuyenDangNhap); //cmd.Parameters.AddWithValue("@ma_khan_cap", TK.MaKhanCap); cmd.Parameters.AddWithValue("@trang_thai", TK.TrangThai); } return(cmd.ExecuteNonQuery() > 0); }
private void btnCapNhat_Click(object sender, EventArgs e) { if (KiemTraDuLieuHopLe()) { if (DialogResult.Yes == MessageBox.Show("Cập nhật tài khoản có mã " + txtMaDangNhap.Text, "Cập nhật nhân viên", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { TaiKhoan_BUS bus = new TaiKhoan_BUS(); clsTaiKhoan_DTO TK = TaoDoiTuongTaiKhoan(); try { if (bus.ThaoTacVoiTaiKhoan(TK, "Update")) { //MessageBox.Show("Cập nhật thành công"); HienThiDSTaiKhoan(); } else { MessageBox.Show("Cập nhật thất bại"); } } catch (Exception) { throw; } TrangThaiBanDau(); } } else { MessageBox.Show(strError, "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); } strError = ""; }
public static clsTaiKhoan_DTO LayTaiKhoan(clsTaiKhoan_DTO taiKhoan) { clsTaiKhoan_DTO tk = null; using (SqlConnection connection = XuLyDuLieu.MoKetNoi) { string query = string.Format("Select * from TaiKhoan where TenTaiKhoan = '{0}' AND MatKhau = '{1}' ", taiKhoan.TenTaiKhoan, taiKhoan.MatKhau); SqlCommand cmd = new SqlCommand(query, connection); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { tk = new clsTaiKhoan_DTO(); tk.TenTaiKhoan = reader["TenTaiKhoan"].ToString(); tk.MatKhau = reader["MatKhau"].ToString(); tk.TrangThai = (int)reader["TrangThai"]; if (reader["LastLogon"] is DateTime) { tk.LastLogon = (DateTime)reader["LastLogon"]; } tk.NhanVien = clsNhanVien_DAO.LayNhanVien(reader["MaNV"].ToString()); tk.LoaiTK = clsPhanLoaiTK_DAO.LayLoaiTK((int)reader["MaPhanLoaiTK"]); } } } return(tk); }
private void btnXacNhan_Click(object sender, EventArgs e) { if (kiemTraTextbox()) { clsTaiKhoan_DTO taiKhoan = new clsTaiKhoan_DTO(); taiKhoan.TenTaiKhoan = txtTenDangNhap.Text; taiKhoan.MatKhau = txtMatKhau.Text; taiKhoan.TrangThai = 0; taiKhoan = clsTaiKhoan_BUS.LayTaiKhoan(taiKhoan); if (taiKhoan == null) { MessageBox.Show("Tên đăng nhập/ Mật khẩu không đúng, vui lòng nhập lại."); } else if (taiKhoan.TrangThai == 0) { MessageBox.Show("Tài khoản đã bị khóa, vui lòng liên hệ Admin để mở khóa."); } else { Validation.nhanVien = taiKhoan; frmMain main = new frmMain(); main.Show(); clsTaiKhoan_BUS.CapNhatDangNhap(taiKhoan.NhanVien.MaNV); Hide(); } } else { MessageBox.Show("Tên đăng nhập/Mật khẩu không được để trống."); } }
string strWarning = ""; // Cảnh báo trường dữ liệu nhập có thể sai. Nhưng vẫn thêm được vào cơ sở dữ liệu. private void btnDongY_Click(object sender, EventArgs e) { if (KiemTraDuLieuHopLe()) { string strThongBao = "Thêm mới nhân viên " + txtHoTen.Text + "\n"; MessageBoxIcon MBIcon = MessageBoxIcon.Question; if (strWarning != "") { strThongBao = strWarning + "\n Vẫn muốn tiếp tục?"; MBIcon = MessageBoxIcon.Warning; } if (DialogResult.Yes == MessageBox.Show(strThongBao, "Xác nhận thêm", MessageBoxButtons.YesNo, MBIcon)) { NhanVien_BUS busNhanVien = new NhanVien_BUS(); TaiKhoan_BUS busTaiKhoan = new TaiKhoan_BUS(); destFileName = TaoDuongDanAnh(); // Lưu đường dẫn ảnh trước khi TaoNhanVien clsNhanVien_DTO NV = TaoDoiTuongNhanVien(); clsTaiKhoan_DTO TK = TaoDoiTuongTaiKhoanNhanVien(); try { if (busNhanVien.ThaoTacVoiDoiTuongNhanVien(NV, "Add") && busTaiKhoan.ThaoTacVoiTaiKhoan(TK, "Add")) { if (KiemTraHinhAnh()) { File.Copy(fd.FileName, destFileName); } //MessageBox.Show("Đã thên nhân viên mới"); TuPhatSinhMaNhanVien(); TrangThaiBanDau(); } else { MessageBox.Show("Thêm thất bại"); } } catch (Exception) { } //TrangThaiBanDau(); } else { //MessageBox.Show("Hủy thao tác"); } } else { MessageBox.Show(strError, "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error); } strError = ""; destFileName = ""; strWarning = ""; }
public clsTaiKhoan_DTO TaoDoiTuongTaiKhoanNhanVien(string maDN, string matKhau, DateTime ngayTao, string maQuyenDangNhap, string maKhanCap, bool trangThai) { clsTaiKhoan_DTO taiKhoan = new clsTaiKhoan_DTO(); taiKhoan.MaDangNhap = maDN; taiKhoan.MatKhau = matKhau; taiKhoan.NgayTao = ngayTao; taiKhoan.MaQuyenDangNhap = maQuyenDangNhap; taiKhoan.MaKhanCap = maKhanCap; taiKhoan.TrangThai = trangThai; return(taiKhoan); }
public void LoadData(string MaNV) { taiKhoan = clsTaiKhoan_BUS.LayTaiKhoan(MaNV); lblMaNV.Text = taiKhoan.NhanVien.MaNV; txtHoTen.Text = taiKhoan.NhanVien.HoTen; pckNgaySinh.Value = taiKhoan.NhanVien.NgaySinh; txtDiaChi.Text = taiKhoan.NhanVien.DiaChi; txtMatKhau.Text = taiKhoan.MatKhau; txtQueQuan.Text = taiKhoan.NhanVien.QueQuan; txtSoDienThoai.Text = taiKhoan.NhanVien.SoDienThoai; lblTenDangNhap.Text = taiKhoan.TenTaiKhoan; lblNgayKhoiTao.Text = taiKhoan.NhanVien.NgayKhoiTao.ToString("dd/MM/yyyy hh:mm:ss"); lblMaNV.Text = taiKhoan.NhanVien.MaNV; lblLastLogon.Text = taiKhoan.LastLogon.ToString("dd/MM/yyyy hh:mm:ss"); cboLoaiTK.SelectedValue = taiKhoan.LoaiTK.MaPhanLoaiTK; pictureHinhAnh.ImageLocation = taiKhoan.NhanVien.HinhAnh.Url; }
private void dgvDSNV_KeyUp(object sender, KeyEventArgs e) { if (dgvDSNV.Rows.Count > 0 && dgvDSNV.SelectedRows.Count > 0) { DataGridViewRow row = dgvDSNV.SelectedRows[0]; int rowIndex = row.Index; if (e.KeyCode == Keys.Delete) { if (DialogResult.Yes == MessageBox.Show("Bạn muốn xóa nhân viên " + txtHoTen.Text + " ?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { clsNhanVien_DTO NV = new clsNhanVien_DTO(); clsTaiKhoan_DTO TK = new clsTaiKhoan_DTO(); TK.MaDangNhap = lblMaNV.Text; TK.TrangThai = false; NV.MaNhanVien = lblMaNV.Text; NV.TrangThai = 4; NhanVien_BUS busNV = new NhanVien_BUS(); TaiKhoan_BUS busTK = new TaiKhoan_BUS(); try { if (busNV.ThaoTacVoiDoiTuongNhanVien(NV, "Delete") && (busTK.XoaTaiKhoanKhiXoaNhanVien(lblMaNV.Text))) { //MessageBox.Show("Đã xóa nhân viên " + txtHoTen.Text, "Thông báo xóa nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Information); HienThiDSNhanVien(); TrangThaiBanDau(); } else { MessageBox.Show("Không thể xóa nhân viên " + txtHoTen.Text, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception) { throw; } dgvDSNV.FirstDisplayedScrollingRowIndex = rowIndex - 1; } } } }
public static object SuaTaiKhoan(clsTaiKhoan_DTO taiKhoan) { using (SqlConnection connection = XuLyDuLieu.MoKetNoi) { //cập nhật vào bảng sản phẩm string query = string.Format("UPDATE TaiKhoan SET MatKhau = @MatKhau where TenTaiKhoan = @TenTaiKhoan"); SqlCommand cmd = new SqlCommand(query, connection); cmd.Parameters.Add("@MatKhau", SqlDbType.NVarChar).Value = taiKhoan.MatKhau; cmd.Parameters.Add("@TenTaiKhoan", SqlDbType.NVarChar).Value = taiKhoan.TenTaiKhoan; cmd.CommandType = CommandType.Text; try { return(cmd.ExecuteNonQuery() >= 1); } catch (SqlException e) { return(e.Message.ToString()); } } }
public List <clsTaiKhoan_DTO> LayDanhSachTaiKhoan(string hoTenNV) { List <clsTaiKhoan_DTO> lstTaiKhoan = new List <clsTaiKhoan_DTO>(); SqlConnection con = ThaoTacDuLieu_DAO.TaoKetNoi(); string query = string.Format("SELECT ma_dang_nhap, mat_khau, ngay_tao, ma_quyen_dang_nhap, ma_khan_cap, trang_thai FROM TaiKhoan, NhanVien WHERE TaiKhoan.ma_dang_nhap = NhanVien.ma_nhan_vien AND NhanVien.ma_trang_thai_nhan_vien != 4 AND ho_ten LIKE N'%{0}%'", hoTenNV); SqlCommand cmd = ThaoTacDuLieu_DAO.TruyVan(query, con); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { clsTaiKhoan_DTO taiKhoan = new clsTaiKhoan_DTO(); if (!reader.IsDBNull(0)) { taiKhoan.MaDangNhap = (string)reader[0]; } if (!reader.IsDBNull(1)) { taiKhoan.MatKhau = (string)reader[1]; } if (!reader.IsDBNull(2)) { taiKhoan.NgayTao = (DateTime)reader[2]; } if (!reader.IsDBNull(3)) { taiKhoan.MaQuyenDangNhap = (string)reader[3]; } if (!reader.IsDBNull(4)) { taiKhoan.MaKhanCap = (string)reader[4]; } if (!reader.IsDBNull(5)) { taiKhoan.TrangThai = (bool)reader[5]; } lstTaiKhoan.Add(taiKhoan); } ThaoTacDuLieu_DAO.DongKetNoi(con); return(lstTaiKhoan); }
private void btnSua_Click(object sender, EventArgs e) { clsHinhAnh_DTO hinhAnh = new clsHinhAnh_DTO(pictureHinhAnh.ImageLocation, clsHinhAnh_DTO.LoaiHinhAnh.Avatar, this.taiKhoan.NhanVien.HinhAnh.MaHinhAnh); if (DaSuaHinh) { // Lưu ảnh vào database clsHinhAnh_BUS.ThemHinhAnh(hinhAnh); } clsNhanVien_DTO nhanVien = new clsNhanVien_DTO(lblMaNV.Text, txtHoTen.Text, pckNgaySinh.Value, txtQueQuan.Text, txtDiaChi.Text, txtSoDienThoai.Text, hinhAnh, DateTime.Now); clsTaiKhoan_DTO taiKhoan = new clsTaiKhoan_DTO(lblTenDangNhap.Text, txtMatKhau.Text, nhanVien, null); object resultTaiKhoan = clsTaiKhoan_BUS.SuaTaiKhoan(taiKhoan); object resultNhanVien = clsNhanVien_BUS.SuaNhanVien(nhanVien); if (resultNhanVien is bool && resultTaiKhoan is bool) { MessageBox.Show("Cập nhật thành công"); } else { MessageBox.Show((string)resultNhanVien); } }
/* * Hàm thêm tài khoản Trả về 2 kết quả: * - Nếu thêm thành công sẽ trả về true * - Thất bại sẽ trả về exception(string) */ public static object ThemTaiKhoan(clsTaiKhoan_DTO taiKhoan) { using (SqlConnection connection = XuLyDuLieu.MoKetNoi) { string query = string.Format( "Insert into TaiKhoan (TenTaiKhoan,MatKhau,TrangThai,MaPhanLoaiTK,MaNV) " + "values (@TenTaiKhoan,@MatKhau,@TrangThai,@MaPhanLoaiTK,@MaNV)"); SqlCommand cmd = new SqlCommand(query, connection); cmd.Parameters.Add("@TenTaiKhoan", SqlDbType.NVarChar).Value = taiKhoan.TenTaiKhoan; cmd.Parameters.Add("@MatKhau", SqlDbType.NVarChar).Value = taiKhoan.MatKhau; cmd.Parameters.Add("@TrangThai", SqlDbType.Int).Value = 1; cmd.Parameters.Add("@MaPhanLoaiTK", SqlDbType.Int).Value = taiKhoan.LoaiTK.MaPhanLoaiTK; cmd.Parameters.Add("@MaNV", SqlDbType.Char).Value = taiKhoan.NhanVien.MaNV; cmd.CommandType = CommandType.Text; try { return(cmd.ExecuteNonQuery() == 1); } catch (SqlException e) { return(e.Message.ToString()); } } }
private void taoTaiKhoan() { showValidateLabel(false); if (kiemTraTextbox()) { // Hop le if (clsTaiKhoan_BUS.KiemTraTaiKhoanDaTonTai(txtDangNhap.Text)) { // tai khoan da ton tai DialogResult = MessageBox.Show("Tên tài khoản đã tồn tại, vui lòng nhập tên khác.", "Thông báo", MessageBoxButtons.OK); if (DialogResult == DialogResult.OK) { txtDangNhap.Text = ""; } } else { // Lưu ảnh vào database clsHinhAnh_DTO hinhAnh = new clsHinhAnh_DTO(pictureHinhAnh.ImageLocation, clsHinhAnh_DTO.LoaiHinhAnh.Avatar); object resultHinhAnh = clsHinhAnh_BUS.ThemHinhAnh(hinhAnh); if (resultHinhAnh is bool) { // Lưu nhân viên vào database clsNhanVien_DTO nhanVien = new clsNhanVien_DTO(Helper.GetTimestamp(DateTime.Now), txtHoTen.Text, pckNgaySinh.Value, txtQueQuan.Text, txtDiaChi.Text, txtSoDienThoai.Text, hinhAnh, DateTime.Now); object resultThemNV = clsNhanVien_BUS.ThemNhanVien(nhanVien); if (resultThemNV is bool) { // Lưu Tài Khoản vào database int maLoaiTK = (int)cboLoaiTK.SelectedValue; string moTa = ((DataRowView)cboLoaiTK.Items[cboLoaiTK.SelectedIndex])["MoTa"].ToString(); clsTaiKhoan_DTO taiKhoan = new clsTaiKhoan_DTO(txtDangNhap.Text, txtMatKhau.Text, nhanVien, new clsPhanLoaiTK_DTO(maLoaiTK, moTa)); object resultThemTaiKhoan = clsTaiKhoan_BUS.ThemTaiKhoan(taiKhoan); if (resultThemTaiKhoan is bool) { if ((bool)resultThemTaiKhoan) { // Copy image file vào folder data/avatar string fileName = Path.GetFileName(pictureHinhAnh.ImageLocation); string destPath = Directory.GetCurrentDirectory() + "\\data\\avatar\\" + fileName; File.Copy(pictureHinhAnh.ImageLocation, destPath, true); MessageBox.Show("Tạo tài khoản thành công"); loadBangTK(); } else { MessageBox.Show("Tạo tài khoản thất bại"); } } else { MessageBox.Show((string)resultThemTaiKhoan); } } else { MessageBox.Show((string)resultThemNV); } } else { MessageBox.Show((string)resultHinhAnh); } } } else { DialogResult = MessageBox.Show("Đã xảy ra lỗi, vui lòng kiểm tra lại thông tin nhập.", "Thông báo", MessageBoxButtons.OK); } }
public bool ThaoTacVoiTaiKhoan(clsTaiKhoan_DTO TK, string command) { TaiKhoan_DAO dao = new TaiKhoan_DAO(); return(dao.ThaoTacVoiTaiKhoan(TK, command)); }
public static object ThemTaiKhoan(clsTaiKhoan_DTO taiKhoan) { return(clsTaiKhoan_DAO.ThemTaiKhoan(taiKhoan)); }
public static clsTaiKhoan_DTO LayTaiKhoan(clsTaiKhoan_DTO taiKhoan) { return(clsTaiKhoan_DAO.LayTaiKhoan(taiKhoan)); }
public static object SuaTaiKhoan(clsTaiKhoan_DTO taiKhoan) { return(clsTaiKhoan_DAO.SuaTaiKhoan(taiKhoan)); }