Database Query - C# Programming Exercise

In this exercise, you need to create a program that displays the data about the books that your previous program has stored in the SQLite database. The goal of this exercise is to learn how to retrieve and display data from a database in a C# application. Through this exercise, you will practice executing SQL queries to retrieve information previously stored in a database, specifically about books (title, author, genre, and summary). The data entered and stored in the previous exercise will be displayed on the console or in a graphical interface, depending on how you decide to present the information.

By completing this exercise, you will learn how to work with SELECT queries in SQLite, how to retrieve data from a database, and how to organize that information in a way that is clear and useful to the user. This exercise is essential for understanding how to interact with databases in C# applications, managing input and output of information effectively and efficiently.

 Category

Access To Relational Databases

 Exercise

Database Query

 Objective

Create a program to display the data about books which your previous program has stored.

 Write Your C# Exercise

// Importing necessary namespaces for SQLite operations and basic functionalities
using System; // For basic functionalities like Console and Exception handling
using System.Data.SQLite; // For SQLite operations

// Define a class to handle the database operations for querying the books
class BookDataQuery
{
    private string connectionString; // Connection string to connect to the SQLite database

    // Constructor to initialize the connection string
    public BookDataQuery(string connectionString)
    {
        this.connectionString = connectionString; // Store the connection string
    }

    // Method to display all books stored in the SQLite database
    public void DisplayBooks()
    {
        // SQL query to select all books from the Books table
        string query = "SELECT * FROM Books;";

        // Establish a connection to the SQLite database
        using (SQLiteConnection conn = new SQLiteConnection(connectionString))
        {
            // Open the connection
            conn.Open();

            // Create a SQLiteCommand to execute the query
            using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
            {
                // Execute the query and get a SQLiteDataReader to read the results
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    // Print each book from the result set
                    Console.WriteLine("Books in the database:");
                    Console.WriteLine("-------------------------------------");
                    while (reader.Read())
                    {
                        // Display each book's data
                        Console.WriteLine($"ID: {reader["Id"]}");
                        Console.WriteLine($"Title: {reader["Title"]}");
                        Console.WriteLine($"Author: {reader["Author"]}");
                        Console.WriteLine($"Genre: {reader["Genre"]}");
                        Console.WriteLine($"Summary: {reader["Summary"]}");
                        Console.WriteLine("-------------------------------------");
                    }
                }
            }
        }
    }
}

// Main class to demonstrate the functionality
class Program
{
    static void Main(string[] args)
    {
        // Connection string to the SQLite database (SQLite creates the database file if it doesn't exist)
        string connectionString = "Data Source=Books.db;Version=3;";

        // Create an instance of BookDataQuery to handle database query operations
        BookDataQuery query = new BookDataQuery(connectionString);

        // Display all books stored in the database
        query.DisplayBooks();
    }
}

 Share this C# exercise

 More C# Programming Exercises of Access To Relational Databases

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Full acces to a database

    In this exercise, you need to create a program that allows the user to enter information about books and browse the existing data. The program should handle the case where t...

  •  Database creation

    In this exercise, you need to create a program that asks the user for information about books (title, author, genre, and summary) and stores this data in an SQLite da...