public static Author Find(int authorId) { MySqlConnection conn = DB.Connection(); conn.Open(); var cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT * FROM authors WHERE id = (@authorId);"; MySqlParameter author_id = new MySqlParameter(); author_id.ParameterName = "@authorId"; author_id.Value = authorId; cmd.Parameters.Add(author_id); var rdr = cmd.ExecuteReader() as MySqlDataReader; string AuthorName = ""; int AuthorId = 0; while (rdr.Read()) { AuthorId = rdr.GetInt32(0); AuthorName = rdr.GetString(1); } Author foundAuthor = new Author(AuthorName, AuthorId); conn.Close(); if (conn != null) { conn.Dispose(); } Console.WriteLine(foundAuthor.GetName()); return(foundAuthor); }
public override bool Equals(System.Object otherAuthor) { if (!(otherAuthor is Author)) { return(false); } else { Author newAuthor = (Author)otherAuthor; bool idEquality = this.GetId() == newAuthor.GetId(); bool nameEquality = this.GetName() == newAuthor.GetName(); return(idEquality && nameEquality); } }
public override bool Equals(System.Object otherAuthor) { if (!(otherAuthor is Author)) { return(false); } else { Author newAuthor = (Author)otherAuthor; bool areIdsEqual = (this.GetId() == newAuthor.GetId()); bool areNameEqual = (this.GetName() == newAuthor.GetName()); return(areIdsEqual && areNameEqual); } }
public static List <Book> SearchTitle(Author newAuthor) { MySqlConnection conn = DB.Connection(); conn.Open(); var cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT books.* FROM authors JOIN books_authors ON (authors.id = books_authors.author_id) JOIN books ON (books_authors.book_id = books.id) WHERE authors.name = (@searchAuthor);"; MySqlParameter searchAuthor = new MySqlParameter(); searchAuthor.ParameterName = "@searchAuthor"; searchAuthor.Value = newAuthor.GetName(); cmd.Parameters.Add(searchAuthor); var rdr = cmd.ExecuteReader() as MySqlDataReader; List <Book> books = new List <Book> { }; while (rdr.Read()) { int bookId = rdr.GetInt32(0); string bookTitle = rdr.GetString(1); int bookCopies = rdr.GetInt32(2); Book newBook = new Book(bookTitle, bookCopies, bookId); books.Add(newBook); } conn.Close(); if (conn != null) { conn.Dispose(); } return(books); }