/// <summary> /// Sửa học sinh /// </summary> /// <param name="hs"></param> /// <returns></returns> public bool suaHocSinh(DTO_TiepNhanHocSinh hs) { try { // Ket noi _conn.Open(); // Query string string SQL = string.Format("UPDATE HSHS SET HOTEN = N'{1}', DIACHI = N'{2}', NGAYSINH = '{3}', GIOITINH = N'{4}', TONGIAO = N'{5}', DANTOC = N'{6}', HOTENCHA=N'{7}',NGHENGHIEPCHA=N'{8}',HOTENME=N'{9}',NGHENGHIEPME=N'{10}',MANAMHOC={11} WHERE MAHS = {0}", hs.MAHS, hs.HOTEN, hs.DIACHI, hs.NGAYSINH, hs.GIOITINH, hs.TONGIAO, hs.DANTOC, hs.HOTENCHA, hs.NGHENGHIEPCHA, hs.HOTENME, hs.NGHENGHIEPME, hs.MANAMHOC); // Command (mặc định command type = text nên chúng ta khỏi fải làm gì nhiều). SqlCommand cmd = new SqlCommand(SQL, _conn); // Query và kiểm tra if (cmd.ExecuteNonQuery() > 0) { return(true); } } catch (Exception e) { } finally { // Dong ket noi _conn.Close(); } return(false); }
/// <summary> /// Thêm học sinh /// </summary> /// <param name="hs"></param> /// <returns></returns> public bool themHocSinh(DTO_TiepNhanHocSinh hs) { try { // Ket noi _conn.Open(); // Query string - vì mình để TV_ID là identity (giá trị tự tăng dần) nên ko cần fải insert ID string SQL = string.Format("INSERT INTO HSHS(HOTEN,DIACHI,NGAYSINH,GIOITINH,TONGIAO,DANTOC,HOTENCHA,NGHENGHIEPCHA,HOTENME,NGHENGHIEPME,MANAMHOC) VALUES (N'{0}',N'{1}','{2}', N'{3}', N'{4}', N'{5}', N'{6}', N'{7}', N'{8}', N'{9}',{10})", hs.HOTEN, hs.DIACHI, hs.NGAYSINH, hs.GIOITINH, hs.TONGIAO, hs.DANTOC, hs.HOTENCHA, hs.NGHENGHIEPCHA, hs.HOTENME, hs.NGHENGHIEPME, hs.MANAMHOC); // Command (mặc định command type = text nên chúng ta khỏi fải làm gì nhiều). SqlCommand cmd = new SqlCommand(SQL, _conn); // Query và kiểm tra if (cmd.ExecuteNonQuery() > 0) { return(true); } } catch (Exception e) { } finally { // Dong ket noi _conn.Close(); } return(false); }
private void btnThemVaoDanhSach_Click(object sender, EventArgs e) { btnSua.Enabled = false; btnXoa.Enabled = false; lblThongBao.Visible = true; int year = DateTime.Now.Year; int dtp = dtpNgaySinh.Value.Year; //if (IsNumber(txtMaHocSinh.Text) == false || (txtMaHocSinh.Text.Length != 4)) //{ // lblThongBao.Text ="Mã học sinh phải là 4 con số!!"; // lblThongBao.Visible = true; //} if (txtHoTenHocSinh.Text.Length == 0 || txtHoTenHocSinh.Text.Length > 40) { lblThongBao.Text = "Họ và tên phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtDiaChi.Text.Length == 0 || txtDiaChi.Text.Length > 40) { lblThongBao.Text = "Địa chỉ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (dtp < 1999 || dtp > 2003) { lblThongBao.Text = "Năm sinh phải từ năm 1999 -> 2003!!"; lblThongBao.Visible = true; } else if (cboGioiTinh.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn giới tính!!"; lblThongBao.Visible = true; } else if (cboTonGiao.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn tôn giáo!!"; lblThongBao.Visible = true; } else if (cboDanToc.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn dân tộc!!"; lblThongBao.Visible = true; } else if (txtHoTenCha.Text.Length == 0 || txtHoTenCha.Text.Length > 40) { lblThongBao.Text = "Họ tên cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepCha.Text.Length == 0 || txtNgheNghiepCha.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtHoTenMe.Text.Length == 0 || txtHoTenMe.Text.Length > 40) { lblThongBao.Text = "Họ tên mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepMe.Text.Length == 0 || txtNgheNghiepMe.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (cboNamHoc.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn năm học!!"; lblThongBao.Visible = true; } else { int x; x = Convert.ToInt32(cboNamHoc.SelectedItem.ToString()); //string ngay = dtpNgaySinh.Value.ToString("MM/dd/yyyy"); lblThongBao.Visible = false; // Tạo DTo DTO_TiepNhanHocSinh hs = new DTO_TiepNhanHocSinh(0, txtHoTenHocSinh.Text, txtDiaChi.Text, dtpNgaySinh.Text, cboGioiTinh.Text, cboTonGiao.Text, cboDanToc.Text, txtHoTenCha.Text, txtNgheNghiepCha.Text, txtHoTenMe.Text, txtNgheNghiepMe.Text, x); // Vì ID tự tăng nên để ID số gì cũng dc // Them if (busHS.themHocSinh(hs)) { MessageBox.Show("Thêm thành công"); dgvHocSinh.DataSource = busHS.getHocSinh(); // refresh datagridview Clear(); } else { MessageBox.Show("Thêm không thành công"); } } }
private void btnSua_Click(object sender, EventArgs e) { int year = DateTime.Now.Year; int dtp = dtpNgaySinh.Value.Year; // Kiểm tra nếu có chọn table rồi DialogResult dlr = MessageBox.Show("Bạn có thật sự muốn sửa hay không?", "Thông Báo!!", MessageBoxButtons.YesNo); if (dlr == DialogResult.Yes) { if (dgvHocSinh.SelectedRows.Count > 0) { if (txtHoTenHocSinh.Text.Length == 0 || txtHoTenHocSinh.Text.Length > 40) { lblThongBao.Text = "Họ và tên phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtDiaChi.Text.Length == 0 || txtDiaChi.Text.Length > 40) { lblThongBao.Text = "Địa chỉ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (dtp < 1999 || dtp > 2003) { lblThongBao.Text = "Năm sinh phải từ năm 1999 -> 2003!!"; lblThongBao.Visible = true; } else if (cboGioiTinh.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn giới tính!!"; lblThongBao.Visible = true; } else if (cboTonGiao.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn tôn giáo!!"; lblThongBao.Visible = true; } else if (cboDanToc.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn dân tộc!!"; lblThongBao.Visible = true; } else if (txtHoTenCha.Text.Length == 0 || txtHoTenCha.Text.Length > 40) { lblThongBao.Text = "Họ tên cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepCha.Text.Length == 0 || txtNgheNghiepCha.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtHoTenMe.Text.Length == 0 || txtHoTenMe.Text.Length > 40) { lblThongBao.Text = "Họ tên mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepMe.Text.Length == 0 || txtNgheNghiepMe.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (cboNamHoc.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn năm học!!"; lblThongBao.Visible = true; } else { // Lấy row hiện tại DataGridViewRow row = dgvHocSinh.SelectedRows[0]; int ID = Convert.ToInt32(row.Cells[0].Value.ToString()); int x = Convert.ToInt32(cboNamHoc.SelectedItem.ToString()); lblThongBao.Visible = false; // Tạo DTo DTO_TiepNhanHocSinh hs = new DTO_TiepNhanHocSinh(ID, txtHoTenHocSinh.Text, txtDiaChi.Text, dtpNgaySinh.Text, cboGioiTinh.Text, cboTonGiao.Text, cboDanToc.Text, txtHoTenCha.Text, txtNgheNghiepCha.Text, txtHoTenMe.Text, txtNgheNghiepMe.Text, x); // Sửa if (busHS.suaHocSinh(hs)) { MessageBox.Show("Sửa thành công"); dgvHocSinh.DataSource = busHS.getHocSinh(); // refresh datagridview Clear(); btnSua.Enabled = false; btnXoa.Enabled = false; } else { MessageBox.Show("Sửa không thành công"); } } } } }
private void btnThemVaoDanhSach_Click(object sender, EventArgs e) { btnSua.Enabled = false; btnXoa.Enabled = false; lblThongBao.Visible = true; string dmy = dtpNgaySinh.Value.ToString("MM/dd/yyyy"); int year = DateTime.Now.Year; int dtp = dtpNgaySinh.Value.Year; if (IsChar(txtHoTenHocSinh.Text) == false) { lblThongBao.Text = "Họ và tên không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (KiemTraHoTen(txtHoTenHocSinh.Text) == false) { lblThongBao.Text = "Họ và tên phải viết hoa chữ cái đầu!!"; lblThongBao.Visible = true; } else if (txtHoTenHocSinh.Text.Length == 0 || txtHoTenHocSinh.Text.Length > 40) { lblThongBao.Text = "Họ và tên phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtDiaChi.Text.Length == 0 || txtDiaChi.Text.Length > 40) { lblThongBao.Text = "Địa chỉ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if ((year - dtp) <= dtoQuyDinh.TuoiMin || (year - dtp) >= dtoQuyDinh.TuoiMax) { lblThongBao.Text = "Năm sinh phải từ năm " + (year - dtoQuyDinh.TuoiMax) + "->" + (year - dtoQuyDinh.TuoiMin) + "!!"; lblThongBao.Visible = true; } else if (cboGioiTinh.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn giới tính!!"; lblThongBao.Visible = true; } else if (cboTonGiao.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn tôn giáo!!"; lblThongBao.Visible = true; } else if (cboDanToc.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn dân tộc!!"; lblThongBao.Visible = true; } else if (IsChar(txtHoTenCha.Text) == false) { lblThongBao.Text = "Họ tên cha không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (KiemTraHoTen(txtHoTenCha.Text) == false) { lblThongBao.Text = "Họ tên cha phải viết hoa chữ cái đầu!!"; lblThongBao.Visible = true; } else if (txtHoTenCha.Text.Length == 0 || txtHoTenCha.Text.Length > 40) { lblThongBao.Text = "Họ tên cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (IsChar(txtNgheNghiepCha.Text) == false) { lblThongBao.Text = "Nghề nghiệp cha không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepCha.Text.Length == 0 || txtNgheNghiepCha.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (IsChar(txtHoTenMe.Text) == false) { lblThongBao.Text = "Họ tên mẹ không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (KiemTraHoTen(txtHoTenMe.Text) == false) { lblThongBao.Text = "Họ tên mẹ phải viết hoa chữ cái đầu!!"; lblThongBao.Visible = true; } else if (txtHoTenMe.Text.Length == 0 || txtHoTenMe.Text.Length > 40) { lblThongBao.Text = "Họ tên mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (IsChar(txtNgheNghiepMe.Text) == false) { lblThongBao.Text = "Nghề nghiệp mẹ không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepMe.Text.Length == 0 || txtNgheNghiepMe.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else { lblThongBao.Visible = false; // Tạo DTo DTO_TiepNhanHocSinh hs = new DTO_TiepNhanHocSinh(0, txtHoTenHocSinh.Text, txtDiaChi.Text, dmy, cboGioiTinh.Text, cboTonGiao.Text, cboDanToc.Text, txtHoTenCha.Text, txtNgheNghiepCha.Text, txtHoTenMe.Text, txtNgheNghiepMe.Text); // Vì ID tự tăng nên để ID số gì cũng dc // Them if (busHS.themHocSinh(hs)) { MessageBox.Show("Thêm thành công"); dgvHocSinh.DataSource = busHS.getHocSinh(); // refresh datagridview Clear(); } else { MessageBox.Show("Thêm không thành công"); } } }
private void btnSua_Click(object sender, EventArgs e) { string dmy = dtpNgaySinh.Value.ToString("MM/dd/yyyy"); int year = DateTime.Now.Year; int dtp = dtpNgaySinh.Value.Year; // Kiểm tra nếu có chọn table rồi DialogResult dlr = MessageBox.Show("Bạn có thật sự muốn sửa hay không?", "Thông Báo!!", MessageBoxButtons.YesNo); if (dlr == DialogResult.Yes) { if (dgvHocSinh.SelectedRows.Count > 0) { if (IsChar(txtHoTenHocSinh.Text) == false) { lblThongBao.Text = "Họ và tên không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (KiemTraHoTen(txtHoTenHocSinh.Text) == false) { lblThongBao.Text = "Họ và tên phải viết hoa chữ cái đầu!!"; lblThongBao.Visible = true; } else if (txtHoTenHocSinh.Text.Length == 0 || txtHoTenHocSinh.Text.Length > 40) { lblThongBao.Text = "Họ và tên phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (txtDiaChi.Text.Length == 0 || txtDiaChi.Text.Length > 40) { lblThongBao.Text = "Địa chỉ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if ((year - dtp) <= dtoQuyDinh.TuoiMin || (year - dtp) >= dtoQuyDinh.TuoiMax) { lblThongBao.Text = "Năm sinh phải từ năm " + (year - dtoQuyDinh.TuoiMax) + "->" + (year - dtoQuyDinh.TuoiMin) + "!!"; lblThongBao.Visible = true; } else if (cboGioiTinh.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn giới tính!!"; lblThongBao.Visible = true; } else if (cboTonGiao.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn tôn giáo!!"; lblThongBao.Visible = true; } else if (cboDanToc.Text.Length == 0) { lblThongBao.Text = "Vui lòng chọn dân tộc!!"; lblThongBao.Visible = true; } else if (IsChar(txtHoTenCha.Text) == false) { lblThongBao.Text = "Họ tên cha không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (KiemTraHoTen(txtHoTenCha.Text) == false) { lblThongBao.Text = "Họ tên cha phải viết hoa chữ cái đầu!!"; lblThongBao.Visible = true; } else if (txtHoTenCha.Text.Length == 0 || txtHoTenCha.Text.Length > 40) { lblThongBao.Text = "Họ tên cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (IsChar(txtNgheNghiepCha.Text) == false) { lblThongBao.Text = "Nghề nghiệp cha không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepCha.Text.Length == 0 || txtNgheNghiepCha.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp cha phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (IsChar(txtHoTenMe.Text) == false) { lblThongBao.Text = "Họ tên mẹ không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (KiemTraHoTen(txtHoTenMe.Text) == false) { lblThongBao.Text = "Họ tên mẹ phải viết hoa chữ cái đầu!!"; lblThongBao.Visible = true; } else if (txtHoTenMe.Text.Length == 0 || txtHoTenMe.Text.Length > 40) { lblThongBao.Text = "Họ tên mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else if (IsChar(txtNgheNghiepMe.Text) == false) { lblThongBao.Text = "Nghề nghiệp mẹ không được có số và ký tự đặc biệt!!"; lblThongBao.Visible = true; } else if (txtNgheNghiepMe.Text.Length == 0 || txtNgheNghiepMe.Text.Length > 40) { lblThongBao.Text = "Nghề nghiệp mẹ phải lớn hơn 0 và nhỏ hơn 40 kí tự!!"; lblThongBao.Visible = true; } else { // Lấy row hiện tại DataGridViewRow row = dgvHocSinh.SelectedRows[0]; int ID = Convert.ToInt32(row.Cells[0].Value.ToString()); lblThongBao.Visible = false; // Tạo DTo DTO_TiepNhanHocSinh hs = new DTO_TiepNhanHocSinh(ID, txtHoTenHocSinh.Text, txtDiaChi.Text, dmy, cboGioiTinh.Text, cboTonGiao.Text, cboDanToc.Text, txtHoTenCha.Text, txtNgheNghiepCha.Text, txtHoTenMe.Text, txtNgheNghiepMe.Text); // Sửa if (busHS.suaHocSinh(hs)) { MessageBox.Show("Sửa thành công"); dgvHocSinh.DataSource = busHS.getHocSinh(); // refresh datagridview Clear(); btnSua.Enabled = false; btnXoa.Enabled = false; } else { MessageBox.Show("Sửa không thành công"); } } } } }
public bool suaHocSinh(DTO_TiepNhanHocSinh hs) { return(dalHocSinh.suaHocSinh(hs)); }
public bool themHocSinh(DTO_TiepNhanHocSinh hs) { return(dalHocSinh.themHocSinh(hs)); }