public Book GetBookById(int bookID)
        {
            Book book = null;

            using (SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Book WHERE BookID = @bookid", Connection))
            {
                cmd.Parameters.Add(new SqlCeParameter("@bookid", bookID));

                using (var results = cmd.ExecuteResultSet(ResultSetOptions.Insensitive))
                {
                    if (results.Read())
                    {
                        CheckOrdinals(results);

                        book = new Book
                        {
                            BookID = results.GetInt32(m_bookOrdinals["BookID"]),
                            AuthorID = results.GetInt32(m_bookOrdinals["AuthorID"]),
                            Title = results.GetString(m_bookOrdinals["Title"])
                        };
                    }
                }
            }

            return book;
        }
Example #2
0
        private void TestCascadingUpdates(List<ITestClass> tests)
        {
            var author = new Author
            {
                Name = "Theodore Geisel"
            };

            foreach (var t in tests)
            {
                t.Insert(author);

                var book = new Book
                {
                    BookType = BookType.Fiction,
                    Title = "Fox in Sox"
                };

                author.Books = new Book[] { book };

                t.Update(author);

                var existing = t.GetAuthorById(author.AuthorID);

                // the book should have been inserted, so it will be at index 0 now
                existing.Books[0].Title = "Green Eggs and Ham";

                // this should cascade update the book title
                t.Update(existing);

                existing = t.GetAuthorById(author.AuthorID);
            }
        }
Example #3
0
        private void TestCascadingInsert(List<ITestClass> tests)
        {
            var testBooks = new Book[]
                {
                    new Book
                    {
                      Title = "CSS: The Missing Manual",
                      BookType = BookType.NonFiction
                    },

                    new Book
                    {
                        Title = "JavaScript: The Missing Manual",
                        BookType = BookType.NonFiction
                    },

                    new Book
                    {
                        Title = "Dreamweaver: The Missing Manual",
                        BookType = BookType.NonFiction
                    },
                };

            // ensures that the entity *and its references* get inserted
            Author a = new Author
            {
                Name = "David McFarland",

                Books = testBooks
            };

            foreach (var t in tests)
            {
                var initialCount = t.GetBookCount();

                t.Insert(a);

                var author = t.GetAuthorById(a.AuthorID);
                var count = t.GetBookCount();

                var diff = count - initialCount;
                // diff should == 3
                if (diff != 3) Debugger.Break();
            }

            // create a new author with the same books - the books should *not* get re-inserted - plus one new book
            List<Book> newList = new List<Book>(testBooks);
            newList.Add(
                new Book
                    {
                        Title = "My Coauthors Book",
                        BookType = BookType.NonFiction
                    }
                    );

            Author a2 = new Author
            {
                Name = "Test CoAuthor",

                Books = newList.ToArray()
            };

            foreach (var t in tests)
            {
                var initialCount = t.GetBookCount();

                t.Insert(a2);

                var author = t.GetAuthorById(a.AuthorID);
                var count = t.GetBookCount();
                var diff = count - initialCount;

                // diff should == 1
                if (diff != 1) Debugger.Break();
            }
        }
        public Book[] GenerateBooks(int count)
        {
            Book[] books = new Book[count];

            var r = new Random(Environment.TickCount);

            for (int i = 0; i < count; i++)
            {
                string title = string.Empty;

                for (int w = 0; w < r.Next(m_maxWordsInTitle) + 1; w++)
                {
                    title += Dictionary[r.Next(Dictionary.Length - 1)];
                    title += ' ';
                }

                books[i] = new Book
                {
                    Title = title.TrimEnd(),
                    BookType = r.Next(1) == 0 ? BookType.Fiction : BookType.NonFiction
                };
            }

            return books;
        }