public bool Update(DayNha dayNha) { try { string query = "SELECT * FROM DayNha"; SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection); table = GetData(); table.PrimaryKey = new DataColumn[] { table.Columns[0] }; DataRow row = table.Rows.Find(dayNha.DayNhaId); if (row != null) { row["tenDayNha"] = dayNha.TenDayNha; row["ghiChu"] = dayNha.GhiChu; } SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(dataAdapter); dataAdapter.Update(table); return(true); } catch { return(false); } }
public bool Insert(DayNha dayNha) { try { string query = "SELECT * FROM DayNha"; SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection); table = GetData(); DataRow row = table.NewRow(); row["tenDayNha"] = dayNha.TenDayNha; row["ghiChu"] = dayNha.GhiChu; table.Rows.Add(row); SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(dataAdapter); dataAdapter.Update(table); return(true); } catch { return(false); } }
private void btnThucHien_Click(object sender, EventArgs e) { switch (temp) { case (int)LuaChon.QuocTich: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { QuocTich quocTich = new QuocTich(); quocTich.TenQuocTich = table.Rows[i][1].ToString(); bUS_QuocTich.Insert(quocTich); } } dgvImport.DataSource = bUS_QuocTich.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.TonGiao: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { TonGiao tonGiao = new TonGiao(); tonGiao.TenTonGiao = table.Rows[i][1].ToString(); bUS_TonGiao.Insert(tonGiao); } } dgvImport.DataSource = bUS_TonGiao.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.VatTu: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { VatTu vattu = new VatTu(); vattu.VatTuId = table.Rows[i][0].ToString(); vattu.TenVatTu = table.Rows[i][1].ToString(); vattu.MoTa = table.Rows[i][2].ToString(); vattu.SoLuong = int.Parse(table.Rows[i][3].ToString()); vattu.GhiChu = table.Rows[i][4].ToString(); bUS_VatTu.Insert(vattu); } } dgvImport.DataSource = bUS_TonGiao.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.TinhThanh: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { TinhThanh tinhthanh = new TinhThanh(); tinhthanh.TenTinhThanh = table.Rows[i][1].ToString(); bUS_TinhThanh.Insert(tinhthanh); } } dgvImport.DataSource = bUS_TinhThanh.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.Phong: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { Phong phong = new Phong(); phong.PhongId = table.Rows[i][0].ToString(); phong.DayNhaId = int.Parse(table.Rows[i][1].ToString()); phong.LoaiPhongId = int.Parse(table.Rows[i][2].ToString()); phong.TenPhong = table.Rows[i][3].ToString(); phong.Tang = int.Parse(table.Rows[i][4].ToString()); phong.GiaPhong = decimal.Parse(table.Rows[i][5].ToString()); bUS_Phong.Insert(phong); } } dgvImport.DataSource = bUS_Phong.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.Lop: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { Lop lop = new Lop(); lop.LopId = int.Parse(table.Rows[i][0].ToString()); lop.TenLop = table.Rows[i][1].ToString(); lop.DonViId = int.Parse(table.Rows[i][2].ToString()); bUS_Lop.Insert(lop); } } dgvImport.DataSource = bUS_Lop.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.DayNha: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { DayNha dayNha = new DayNha(); dayNha.TenDayNha = table.Rows[i][1].ToString(); dayNha.GhiChu = table.Rows[i][2].ToString(); bUS_DayNha.Insert(dayNha); } } dgvImport.DataSource = bUS_DayNha.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.DonVi: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { DonVi donVi = new DonVi(); donVi.TenDonVi = table.Rows[i][1].ToString(); donVi.DiaChi = table.Rows[i][2].ToString(); donVi.Sdt = table.Rows[i][3].ToString(); donVi.GhiChu = table.Rows[i][4].ToString(); bUS_DonVi.Insert(donVi); } } dgvImport.DataSource = bUS_DonVi.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.LoiViPham: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { LoiViPham loiViPham = new LoiViPham(); loiViPham.TenLoiViPham = table.Rows[i][1].ToString(); loiViPham.NoiDung = table.Rows[i][2].ToString(); loiViPham.HinhThucXuLy = table.Rows[i][3].ToString(); loiViPham.GhiChu = table.Rows[i][4].ToString(); bUS_LoiViPham.Insert(loiViPham); } } dgvImport.DataSource = bUS_LoiViPham.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.LoaiDoiTuong: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { LoaiDoiTuong loaiDoiTuong = new LoaiDoiTuong(); loaiDoiTuong.TenLoaiDoiTuong = table.Rows[i][1].ToString(); bUS_LoaiDoiTuong.Insert(loaiDoiTuong); } } dgvImport.DataSource = bUS_LoaiDoiTuong.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.LoaiPhong: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { LoaiPhong loaiPhong = new LoaiPhong(); loaiPhong.TenLoaiPhong = table.Rows[i][1].ToString(); loaiPhong.SoLuongtoiDa = int.Parse(table.Rows[i][2].ToString()); loaiPhong.GiaLoaiPhong = decimal.Parse(table.Rows[i][3].ToString()); bUS_LoaiPhong.Insert(loaiPhong); } } dgvImport.DataSource = bUS_LoaiPhong.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.NguoiDung: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { NguoiDung nguoiDung = new NguoiDung(); nguoiDung.TenDangNhap = table.Rows[i][1].ToString(); nguoiDung.MatKhau = table.Rows[i][2].ToString(); nguoiDung.TenDayDu = table.Rows[i][3].ToString(); nguoiDung.Sdt = table.Rows[i][4].ToString(); nguoiDung.DiaChi = table.Rows[i][5].ToString(); bUS_NguoiDung.Insert(nguoiDung); } } dgvImport.DataSource = bUS_NguoiDung.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; case (int)LuaChon.DanToc: if (table != null) { for (int i = 0; i < table.Rows.Count; i++) { DanToc danToc = new DanToc(); danToc.TenDanToc = table.Rows[i][1].ToString(); bUS_DanToc.Insert(danToc); } } dgvImport.DataSource = bUS_DanToc.GetData(); dgvImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; break; default: break; } }
private void btnSave_Click(object sender, EventArgs e) { if (txtTenDayNha.Text == "") { MessageBox.Show("Dữ liệu nhập chưa đủ."); errorProvider1.SetError(txtTenDayNha, "Chưa điền tên."); } else { DayNha daynha = new DayNha(); if (chucnang == 1) { daynha.TenDayNha = txtTenDayNha.Text; daynha.GhiChu = txtGhiChu.Text; if (bUS_DayNha.Insert(daynha)) { //------------Ghi log NhatKyHoatDong nhatKy = new NhatKyHoatDong(); nhatKy.NguoiDungId = Const.CurrentUser.NguoiDungId; nhatKy.NoiDung = "Thêm thành công dãy nhà " + txtTenDayNha.Text + " vào hệ thống"; nhatKy.ThaoTac = "Tạo"; nhatKy.ThoiGian = DateTime.Now; nhatKy.ChucNang = "Dãy nhà"; Const.NhatKyHoatDong.Insert(nhatKy); //------------------- MessageBox.Show("Thêm dữ liệu thành công.", "Thông báo."); } else { MessageBox.Show("Thêm dữ liệu lỗi.", "Thông báo."); } } if (chucnang == 2) { daynha.DayNhaId = int.Parse(txtDayNhaId.Text); daynha.TenDayNha = txtTenDayNha.Text; daynha.GhiChu = txtGhiChu.Text; if (bUS_DayNha.Update(daynha)) { //------------Ghi log NhatKyHoatDong nhatKy = new NhatKyHoatDong(); nhatKy.NguoiDungId = Const.CurrentUser.NguoiDungId; nhatKy.NoiDung = "Cập nhập thành công dãy nhà " + txtTenDayNha.Text + " trong hệ thống"; nhatKy.ThaoTac = "Cập nhập"; nhatKy.ThoiGian = DateTime.Now; nhatKy.ChucNang = "Dãy nhà"; Const.NhatKyHoatDong.Insert(nhatKy); //------------------- MessageBox.Show("Cập nhật dữ liệu thành công.", "Thông báo."); } else { MessageBox.Show("Cập nhật dữ liệu lỗi.", "Thông báo."); } } reset(); } }