public static List<Book> GetBooks(bool useDB) { try { DataTable dt; List<Book> books = new List<Book>(); if (useDB) { dt = DBBookshop.GetBooks(cs); } else { dt = DBBookshop.GetTestData(); } Book book; foreach (DataRow row in dt.Rows) { book = new Book((int)row[0]); book.Name = row["name"].ToString(); book.Author = row["author"].ToString(); book.Country = row["country"].ToString(); book.Year = (int)row["year"]; books.Add(book); } return books; } catch (Exception ex) { throw ex; } }
private void btnCreateNew_Click(object sender, RoutedEventArgs e) { if (btnCreateNew.Content.ToString() == "Uusi") { //luodaan uusi kirja-olio Book newBook = new Book(0); newBook.Name = "Anna kirjan nimi"; spInfo.DataContext = newBook; btnCreateNew.Content = "Tallenna uusi kantaan"; } else { //tallennetaan Book current = (Book)spInfo.DataContext; Bookshop.InsertBook(current); dgBooks.DataContext = Bookshop.GetBooks(true); MessageBox.Show(string.Format("Kirja {0} tallennettu kantaan onnistuneesti", current.ToString())); } }
private void btnNew_Click(object sender, RoutedEventArgs e) { if (btnNew.Content.ToString() == "New") { // Luodaan uusi olio Book newBook = new Book(0); newBook.Name = "Anna kirjan nimi"; spContent.DataContext = newBook; btnNew.Content = "Insert to database"; } else { // Tallennetaan Book current = (Book)spContent.DataContext; Bookshop.InsertBook(current); dgBooks.DataContext = Bookshop.GetBooks(true); MessageBox.Show(string.Format("Book {0} inserted to database successfully", current.ToString())); } }
public static List<Book> GetBooks(bool useDB) { try { DataTable dt; List<Book> books = new List<Book>(); if (useDB) { // Pyydetään DB-kerrokselta kirjojen tiedot dt = DBBookshop.GetBooks(cs); } else { dt = DBBookshop.GetTestData(); } // ORM = muutetaan datatablen rivit olioiksi Book book; foreach (DataRow row in dt.Rows) { book = new Book((int)row[0]); book.Name = row["name"].ToString(); book.Author = row["author"].ToString(); book.Country = row["country"].ToString(); book.Year = (int)row["year"]; books.Add(book); } return books; } catch (Exception ex) { throw ex; } }
public static bool DeleteBook(Book book) { try { int lkm = DBBookshop.DeleteBook(cs, book.ID); if (lkm > 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } }
public static bool InsertBook(Book book) { try { int lkm = DBBookshop.InsertBook(cs, book.Name, book.Author, book.Country, book.Year); if (lkm > 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } }
public static int UpdateBook(Book book) { try { int lkm = DBBookshop.UpdateBook(cs, book.ID, book.Name, book.Author, book.Country, book.Year); return lkm; } catch (Exception ex) { throw ex; } }