private static void ListingAllBooks() { Console.WriteLine("Listing all books: "); MySqlCommand mySqlCommand = new MySqlCommand(@"SELECT Title, Author, PublishDate, ISBN FROM Books", mySqlConnection); using (MySqlDataReader reader = mySqlCommand.ExecuteReader()) { while (reader.Read()) { var title = reader["Title"].ToString(); var author = reader["Author"].ToString(); var publishDate = (DateTime)reader["PublishDate"]; var isbn = reader["ISBN"].ToString(); var book = new Book() { Title = title, Author = author, PublishDate = publishDate, ISBN = isbn }; Console.WriteLine(book); } } }
private static void FindBooksByName(string substring) { Console.WriteLine("\nFinding books by name '{0}':", substring); MySqlCommand mySqlCommand = new MySqlCommand(@"SELECT Title, Author, PublishDate, ISBN FROM Books WHERE LOCATE(@substring, Title)", mySqlConnection); mySqlCommand.Parameters.AddWithValue("@substring", substring); using (MySqlDataReader reader = mySqlCommand.ExecuteReader()) { while (reader.Read()) { var title = reader["Title"].ToString(); var author = reader["Author"].ToString(); var publishDate = (DateTime)reader["PublishDate"]; var isbn = reader["ISBN"].ToString(); var book = new Book() { Title = title, Author = author, PublishDate = publishDate, ISBN = isbn }; Console.WriteLine(book); } } }