// Thêm tác giả public bool Add(TacGia value) { try { _db.TacGia.Add(value); _db.SaveChanges(); return true; } catch (Exception e) { Console.WriteLine(e.Message); return false; } }
private void btnAdd_Click(object sender, RoutedEventArgs e) { if (!CheckNull()) return; var record = new TacGia() { TenTacGia = txtTenTacGia.Text, NoiCongTac = txtNoiCongTac.Text }; if (db.Add(record)) { MessageBox.Show("Thêm thành công"); btnResetS_Click(null, null); tacGiaDataGrid.SelectedIndex = tacGiaDataGrid.Items.Count - 1; tacGiaDataGrid.ScrollIntoView(record); btnAdd.IsEnabled = false; } else MessageBox.Show("Thêm thất bại"); }
// Cập nhật tác giả public bool Update(TacGia value) { try { TacGia record = _db.TacGia.SingleOrDefault(v => v.MaTacGia == value.MaTacGia); record.TenTacGia = value.TenTacGia; record.NoiCongTac = value.NoiCongTac; _db.SaveChanges(); return true; } catch (Exception e) { Console.WriteLine(e.Message); return false; } }
private bool WriteExcel(TacGia record) { try { COMExcel.Application exApp = new COMExcel.Application(); string workbookPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Resource\BCTacGia.xlsx"); COMExcel.Workbook exBook = exApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1]; ((Microsoft.Office.Interop.Excel._Worksheet)exSheet).Activate(); exSheet.Cells[3, 3] = record.MaTacGia.ToString(); exSheet.Cells[4, 3] = record.NoiCongTac; exSheet.Cells[3, 5] = record.TenTacGia; exSheet.Cells[4, 5] = record.DauSach.Count; // Xuất danh sách int i = 1; foreach (DauSach item in record.DauSach) { exSheet.Cells[7 + i, 1] = i; exSheet.Cells[7 + i, 2] = item.MaDauSach; exSheet.Cells[7 + i, 3] = item.TenDauSach; exSheet.Cells[7 + i, 4] = item.LoaiSach.TenLoai; exSheet.Cells[7 + i, 5] = item.NXB.TenNXB; exSheet.Cells[7 + i, 6] = item.TrangThaiDauSach.TenTrangThai; i++; } // i = i + 6; COMExcel.Range r = (COMExcel.Range)exSheet.get_Range("A8", "F" + i); r.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; r.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black); // Lưu file SaveFileDialog dialog = new SaveFileDialog { FileName = "BCTacGia.xls", Filter = "Excel files|*.xls", DefaultExt = "xls", Title = "Chọn nơi lưu tệp báo cáo" }; if (dialog.ShowDialog() == true) { exBook.SaveAs(dialog.FileName, COMExcel.XlFileFormat.xlWorkbookNormal, null, null, false, false, COMExcel.XlSaveAsAccessMode.xlExclusive, false, false, false, false, false); MessageBox.Show("Xuất báo cáo thành công"); } // exBook.Close(false, false, false); exApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp); return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } }