private void btnNew_Click(object sender, RoutedEventArgs e) { try { if(btnNew.Content.ToString() == "Uusi") { Book newBook = new Book(0); newBook.Name = "Anna kirjan nimi"; spBook.DataContext = newBook; btnNew.Content = "Tallenna uusi kantaan"; } else { //Tallennetaan Book current = (Book)spBook.DataContext; Bookshop.InsertBook(current); myGrid.DataContext = Bookshop.GetBooks(true); MessageBox.Show(string.Format("Kirja {0} tallennettu kantaan onnistuneesti", current.ToString())); btnNew.Content = "Uusi"; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnUusi_Click(object sender, RoutedEventArgs e) { if (btnUusi.Content.ToString() == "Uusi") { //luodaan uusi kirja -olio Book newBook = new Book(0); newBook.Name = "Anna kirjan nimi"; spBook.DataContext = newBook; btnUusi.Content = "Tallenna uusi kantaan"; } else { //tallennetaan Book current = (Book)spBook.DataContext; Bookshop.InsertBook(current); dgBooks.DataContext = Bookshop.GetBooks(true); tbMessage.Text = string.Format("Kirja {0} tallennettu kantaan onnistuneesti", current.ToString()); btnUusi.Content = "Uusi"; } }
public static List<Book> GetBooks(bool useDB) { try { DataTable dt = new DataTable(); 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); } //palautus 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; } }
public static List <Book> getBooks(Boolean useDB) { try { DataTable dt; List<Book> temp = new List<Book>(); //haetaan kirjoja, db-kerroksen palauttama datatable mapataan olioiksi if (useDB) { dt = DBBooks.GetBooks(cs); } else { dt = DBBooks.GetTestData(); } //tehdään ORM eli DataTablen rivit muutetaan olioiksi Book book; foreach (DataRow dr in dt.Rows) { book = new Book((int)dr[0]); book.Author = dr["author"].ToString(); book.Name = dr["name"].ToString(); book.Country = dr["country"].ToString(); book.Year = (int)dr["year"]; //olio lisätään kokoelmaan temp.Add(book); } return temp; } catch (Exception ex) { throw ex; } }
public static void UpdateBook(Book book) { try { int lkm = DBBooks.UpdateBook(cs, book.Id, book.Name, book.Author, book.Country, book.Year); } catch ( Exception ex) { throw ex; } }