Beispiel #1
0
        private static void FindBookByName(string connectionString)
        {
            MySqlConnection dbConnection = new MySqlConnection(connectionString);

            dbConnection.Open();
            Console.WriteLine();
            Console.WriteLine("***Enter a name of a book to search***");
            string input = Console.ReadLine();

            using (dbConnection)
            {
                MySqlCommand command = new MySqlCommand(string.Format(@"SELECT Title, Author, PublishDate, ISBN FROM books
                                                                      WHERE Title LIKE '%{0}%'",
                                                                      input),
                                                        dbConnection);

                MySqlDataReader reader = command.ExecuteReader();

                using (reader)
                {
                    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);
                    }
                }
            }
        }
Beispiel #2
0
        private static void ListAllBooks(string connectionString)
        {
            MySqlConnection dbConnection = new MySqlConnection(connectionString);

            dbConnection.Open();
            Console.WriteLine("***Listing all books***");

            using (dbConnection)
            {
                MySqlCommand command = new MySqlCommand(
                                                        @"SELECT Title, Author, PublishDate, ISBN
                                                        FROM Books",
                                                        dbConnection);
                using (var reader = command.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);
                    }
                }
            }
        }