コード例 #1
0
 public bool AddNewBook(Book book)
 {
     bool flag;
     try
     {
         if (GetBook(book.Isbn) == null)
         {
             source.TraceInformation("Adding book:" + book.Name);
             dc.Booktable.InsertOnSubmit(book);
             dc.SubmitChanges();
             source.TraceInformation("Added book:" + book.Name);
             flag = true;
         }
         else
         {
             throw new Exception("Book already exist");
         }
     }
     catch (Exception e)
     {
         source.TraceEvent(TraceEventType.Error, 0, e.ToString());
         flag = false;
     }
     source.Flush();
     return flag;
 }
コード例 #2
0
 public bool AddNewBook(Book book)
 {
     try
     {
         conn.Open();
         string insertString = @"insert into tbl_book(isbn, name, author, description) values ('"+book.Isbn+"', '"+book.Name+"', '"+book.Author+"', '"+book.Description+"')";
         SqlCommand cmd = new SqlCommand(insertString, conn);
         cmd.ExecuteNonQuery();
     }
     finally
     {
         if (conn != null)
         {
             conn.Close();
         }
     }
     return true;
 }
コード例 #3
0
        public List<Book> GetAllBooks()
        {
            SqlDataReader rdr = null;
            List<Book> books = new List<Book>();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from tbl_book", conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    Book b = new Book();
                    b.Isbn = ((string)rdr[0]).Trim();
                    b.Name = ((string)rdr[1]).Trim();
                    b.Author = ((string)rdr[2]).Trim();
                    b.Description = ((string)rdr[3]).Trim();
                    if (rdr[4] != DBNull.Value)
                    {
                        b.Borrowed_by = ((string)rdr[4]).Trim();
                    }
                    if (rdr[5] != DBNull.Value)
                    {
                        b.Borrowed_date = ((string)rdr[5]).Trim();
                    }

                    books.Add(b);
                }

            }
            finally
            {
                if (rdr != null)
                {
                    rdr.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return books;
        }
コード例 #4
0
 public bool UpdateBook(Book book)
 {
     try
     {
         conn.Open();
         string updateString = @"update tbl_book set name = '"+book.Name+"', author = '"+book.Author+"', description = '"+book.Description+"' where isbn = '"+book.Isbn+"'";
         SqlCommand cmd = new SqlCommand(updateString, conn);
         cmd.ExecuteNonQuery();
     }
     finally
     {
         if (conn != null)
         {
             conn.Close();
         }
     }
     return true;
 }
コード例 #5
0
        public Book GetBook(string isbn)
        {
            SqlDataReader rdr = null;
            Book book = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from tbl_book where isbn = '"+isbn+"'", conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    book = new Book();
                    book.Isbn = ((string)rdr[0]).Trim();
                    book.Name = ((string)rdr[1]).Trim();
                    book.Author = ((string)rdr[2]).Trim();
                    book.Description = ((string)rdr[3]).Trim();
                    if (rdr[4] != DBNull.Value) {
                        book.Borrowed_by = ((string)rdr[4]).Trim();
                    }
                    if (rdr[5] != DBNull.Value) {
                        book.Borrowed_date = ((string)rdr[5]).Trim();
                    }
                }

            }
            finally
            {
                if (rdr != null)
                {
                    rdr.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return book;
        }
コード例 #6
0
 public bool UpdateBook(Book book)
 {
     return das.UpdateBook(book);
 }
コード例 #7
0
 public bool AddNewBook(Book book)
 {
     return das.AddNewBook(book);
 }
コード例 #8
0
 public bool UpdateBook(Book book)
 {
     bool flag;
     try
     {
         Book toupdate = GetBook(book.Isbn);
         if (toupdate != null)
         {
             source.TraceInformation("Updating book:" + book.Name);
             Book entityBook = dc.Booktable.Single(b => b.Isbn == book.Isbn);
             entityBook.Name = book.Name;
             entityBook.Author = book.Author;
             entityBook.Description = book.Description;
             dc.SubmitChanges();
             source.TraceInformation("Updateded book:" + book.Name);
             flag = true;
         }
         else
         {
             throw new Exception("Wrong Isbn ....");
         }
     }
     catch (Exception e)
     {
         source.TraceEvent(TraceEventType.Error, 0, e.ToString());
         flag = false;
     }
     source.Flush();
     return flag;
 }
コード例 #9
0
        public bool AddNewBook(string isbn, string name, string author, string description)
        {
            if(isbn == ""){
                throw new Exception("ISBN is required...!");
            }
            if (ls.GetBook(isbn) != null)
            {
                throw new Exception("ISBN already exist...!");
            }
            //if (name == "")
            //{
            //    throw new Exception("Name is required...!");
            //}
            //if (author == "")
            //{
            //    throw new Exception("Author is required...!");
            //}
            //if (description == "")
            //{
            //    throw new Exception("Description is required...!");
            //}

            Book book = new Book();
            book.Isbn = isbn;
            book.Name = name;
            book.Author = author;
            book.Description = description;
            return ls.AddNewBook(book);
        }
コード例 #10
0
        public void TestAddBookAsBook()
        {
            TestSetup();
            LibraryClient lc = new LibraryClient();
            Book expectedBook = new Book() { Isbn = "01", Author = "author1", Description = "desc1", Name = "name1" };
            Assert.IsTrue(lc.AddNewBook(expectedBook));

            Book actualBook = lc.GetBook("01");
            Assert.AreEqual(expectedBook.Author, actualBook.Author);
            Assert.AreEqual(expectedBook.Name, actualBook.Name);
            Assert.AreEqual(expectedBook.Description, actualBook.Description);
        }