public List <Book> GetIssuedBooks() { DatabasePackage dbPackage = new DatabasePackage(_ConnectionString); List <Book> books = new List <Book>(); SqlDataReader reader = null; try { dbPackage.CommandText = "Select ID, Name, IssueDate, MemberID From Book Where MemberID is Not Null Order By Name"; reader = dbPackage.GetReader(); while (reader.Read()) { Book book = new Book(); book.ID = dbPackage.FieldToInt(reader["ID"]); book.Name = dbPackage.FieldToString(reader["Name"]); book.IssueDate = dbPackage.FieldToDateTime(reader["IssueDate"]); book.MemberID = dbPackage.FieldToInt(reader["MemberID"]); books.Add(book); } } catch (DataAccessException ex) { throw new DataLayerException("Collection could not populated.", ex); } catch (Exception ex) { throw new DataLayerException("Collection could not populated.", ex); } finally { if (reader != null) { reader.Close(); } dbPackage.CloseConnection(); } return(books); }
public Book GetBookByID(int id) { DatabasePackage dbPackage = new DatabasePackage(_ConnectionString); SqlDataReader reader = null; try { dbPackage.CommandText = "Select ID, Name, IssueDate, MemberID From Book Where ID = " + id; reader = dbPackage.GetReader(); reader.Read(); Book book = new Book { ID = id, Name = dbPackage.FieldToString(reader["Name"]), IssueDate = dbPackage.FieldToDateTime(reader["IssueDate"]), MemberID = dbPackage.FieldToInt(reader["MemberID"]) }; return(book); } catch (DataAccessException ex) { throw new DataLayerException("Unable to retrieve data.", ex); } catch (Exception ex) { throw new DataLayerException("Unable to retrieve data.", ex); } finally { if (reader != null) { reader.Close(); } } }