private static void ListingAllBooks() { Console.WriteLine("Listing all books: "); var mySqlCommand = new MySqlCommand(@"SELECT Title, Author, PublishDate, ISBN FROM Books", mySqlConnection); using (var reader = mySqlCommand.ExecuteReader()) { while (reader.Read()) { var title = reader["Title"].ToString(); var author = reader["Author"].ToString(); var publishDate = DateTime.Parse(reader["PublishDate"].ToString()); 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); var mySqlCommand = new MySqlCommand(@"SELECT Title, Author, PublishDate, ISBN FROM Books WHERE LOCATE(@substring, Title)", mySqlConnection); mySqlCommand.Parameters.AddWithValue("@substring", substring); using (var reader = mySqlCommand.ExecuteReader()) { while (reader.Read()) { var title = reader["Title"].ToString(); var author = reader["Author"].ToString(); var publishDate = DateTime.Parse(reader["PublishDate"].ToString()); var isbn = reader["ISBN"].ToString(); var book = new Book { Title = title, Author = author, PublishDate = publishDate, Isbn = isbn }; Console.WriteLine(book); } } }