Book Image

The C# Workshop

By : Jason Hales, Almantas Karpavicius, Mateus Viegas
4 (2)
Book Image

The C# Workshop

4 (2)
By: Jason Hales, Almantas Karpavicius, Mateus Viegas

Overview of this book

C# is a powerful, versatile language that can unlock a variety of career paths. But, as with any programming language, learning C# can be a challenging process. With a wide range of different resources available, it’s difficult to know where to start. That's where The C# Workshop comes in. Written and reviewed by industry experts, it provides a fast-paced, supportive learning experience that will quickly get you writing C# code and building applications. Unlike other software development books that focus on dry, technical explanations of the underlying theory, this Workshop cuts through the noise and uses engaging examples to help you understand how each concept is applied in the real world. As you work through the book, you'll tackle realistic exercises that simulate the type of problems that software developers work on every day. These mini-projects include building a random-number guessing game, using the publisher-subscriber model to design a web file downloader, creating a to-do list using Razor Pages, generating images from the Fibonacci sequence using async/await tasks, and developing a temperature unit conversion app which you will then deploy to a production server. By the end of this book, you'll have the knowledge, skills, and confidence to advance your career and tackle your own ambitious projects with C#.
Table of Contents (10 chapters)

.NET Command-Line Interface (CLI)

Once you have installed .NET, the CLI can be used to create and configure projects for use with VS Code. To launch the .NET CLI, run the following at the command prompt:

dotnet

If .NET is installed correctly, you will see the following message on your screen:

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Once you have the CLI installed to configure projects with VS Code, you need to know about the powerful open source object-relational database system that uses and extends the SQL language that is, PostgreSQL.

Note

You will first go through the instructions to install PostgreSQL for Windows followed by macOS, and then by Linux.

PostgreSQL Installation for Windows

PostgreSQL has been used in Chapter 6, Entity Framework with SQL Server. Before you proceed with that chapter, you must install PostgreSQL on your system using the following steps:

  1. Go to https://www.enterprisedb.com/downloads/postgres-postgresql-downloads and download the latest version installer for Windows:
Figure 0.8: Latest PostgreSQL versions for each platform

Figure 0.8: Latest PostgreSQL versions for each platform

Note

The screen shown in Figure 0.8 may change depending upon the latest release from the vendor.

  1. Open the downloaded interactive installer and click the Next button. The Setup PostgreSQL screen gets displayed:
Figure 0.9: Welcome screen for PostgreSQL upload

Figure 0.9: Welcome screen for PostgreSQL upload

  1. Click on the Next button to move to the next screen which asks for the installation directory details:
Figure 0.10: PostgreSQL default installation directory

Figure 0.10: PostgreSQL default installation directory

  1. Leave the default Installation Directory unchanged and click the Next button.
  2. Select the following from the list in Figure 0.11:
    • PostgreSQL Server refers to the database.
    • pgAdmin 4 is the database management tool.
    • Stack Builder is the PostgreSQL environment builder (optional).
    • Command Line Tools work with the database using a command line.
Figure 0.11: The PostgreSQL components selected to proceed

Figure 0.11: The PostgreSQL components selected to proceed

  1. Then click the Next button.
  2. In the next screen, the Data Directory screen asks you to enter the directory for storing your data. So, enter the data directory name:
Figure 0.12: The directory for storing data

Figure 0.12: The directory for storing data

  1. Once you have entered the data directory, click on the Next button to continue. The next screen asks you to enter the password.
  2. Enter the new Password.
  3. Retype the password beside Retype password for the database superuser:
Figure 0.13: Providing password for database superuser

Figure 0.13: Providing password for database superuser

  1. Then click the Next button to continue.
  2. The next screen displays the Port as 5432. Use the default port—that is, 5432:
Figure 0.14: Selecting Port

Figure 0.14: Selecting Port

  1. Click the Next button.
  2. The Advanced Options screen asks you to type the locale for the Database cluster. Leave it as [Default locale]:
Figure 0.15: Selecting the locale for the Database cluster

Figure 0.15: Selecting the locale for the Database cluster

  1. Then click the Next button.
  2. When the Preinstallation Summary screen gets displayed, click the Next button to go ahead:
Figure 0.16: Setup window showing ready to install message

Figure 0.16: Setup window showing ready to install message

  1. Continue selecting the Next button (leaving the default settings unchanged) until the installation process begins.
  2. Wait for it to complete. On completion, the Completing the PostgreSQL Setup Wizard screen gets displayed.
  3. Uncheck the Launch Stack Builder at exit option:
Figure 0.17: Installation complete with Stack Builder unchecked

Figure 0.17: Installation complete with Stack Builder unchecked

The Stack Builder is used to download and install additional tools. The default installation contains all tools needed for the exercises and activities.

  1. Finally, click the Finish button.
  2. Now open pgAdmin4 from Windows.
  3. Enter a master Password for connecting to any database inside PostgreSQL in the Set Master Password window:
Figure 0.18: Setting Master Password for connecting to a PostgreSQL server

Figure 0.18: Setting Master Password for connecting to a PostgreSQL server

Note

It is better to type a password that you can easily memorize as it will be used to manage all your other credentials.

  1. Next click the OK button.
  2. On the left side of the pgadmin window, expand the Server by clicking the arrow beside it.
  3. You will be asked to enter your PostgreSQL server password. Type the same password that you entered in Step 22.
  4. Do not click Save password for security reasons:
Figure 0.19: Setting the postgres user password for the PostgreSQL server

Figure 0.19: Setting the postgres user password for the PostgreSQL server

PostgreSQL server password is the password you will use when connecting to the PostgreSQL server and using the postgres user.

  1. Finally click the OK button. You will see the pgAdmin dashboard:
Figure 0.20: pgAdmin 4 dashboard window

Figure 0.20: pgAdmin 4 dashboard window

In order to explore the pgAdmin dashboard, move to the Exploring pgAdmin Dashboard section.

PostgreSQL Installation for macOS

Install PostgreSQL on your macOS using the following steps:

  1. Visit the official site of the Postgres app to download and install PostgreSQL on your mac platform: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads.
  2. Download the latest PostgreSQL for macOS:

    Note

    The following screenshots were taken for version 14.4 on macOS Monterey (version 12.2).

Figure 0.21: Installation page for PostgreSQL

Figure 0.21: Installation page for PostgreSQL

  1. Once you have downloaded the installer file for macOS, double-click the installer file to launch the PostgreSQL Setup Wizard:
Figure 0.22: Launching the PostgreSQL setup wizard

Figure 0.22: Launching the PostgreSQL setup wizard

  1. Select the location where you want PostgreSQL installed:
Figure 0.23: Selecting the installation directory

Figure 0.23: Selecting the installation directory

  1. Click on the Next button.
  2. In the next screen, select the following components for installation:
    • PostgreSQL Server
    • pgAdmin 4
    • Command Line Tools
  3. Uncheck the Stack Builder component:
Figure 0.24: Selecting the components for installation

Figure 0.24: Selecting the components for installation

  1. Once you have selected the options, click on the Next button.
  2. Specify the data directory in which PostgreSQL will store the data:
Figure 0.25: Specifying the data directory

Figure 0.25: Specifying the data directory

  1. Click on the Next button.
  2. Now set a Password for the Postgres database superuser:
Figure 0.26: Setting the password

Figure 0.26: Setting the password

Make sure to note down the password safely for logging in to the PostgreSQL database.

  1. Click on the Next button.

Set the port number where you want to run the PostgreSQL server. Here the default Port number is set as 5432:

Figure 0.27: Specifying the port number

Figure 0.27: Specifying the port number

  1. Click on the Next button.
  2. Select the locale to be used by PostgreSQL. Here, [Default locale] is the locale selected for macOS:
Figure 0.28: Selecting the locale specification

Figure 0.28: Selecting the locale specification

  1. Click on the Next button.
  2. In the next screen, check the installation details:
Figure 0.29: Pre Installation summary page

Figure 0.29: Pre Installation summary page

Finally, click on the Next button to start the installation process of the PostgreSQL database server on your system:

Figure 0.30: Ready to Install page before starting the installation process

Figure 0.30: Ready to Install page before starting the installation process

  1. Wait for a few moments for the installation process to complete:
Figure 0.31: Setup Installation in progress

Figure 0.31: Setup Installation in progress

  1. When prompted, click the Next button. The next screen displays the message that the PostgreSQL installation is complete on your system:
Figure 0.32: Success message showing the setup as complete

Figure 0.32: Success message showing the setup as complete

  1. Click the Finish button once the installation gets complete.
  2. Now load the database in the PostgreSQL server.
  3. Double-click on the pgAdmin 4 icon to launch it from your Launchpad.
  4. Enter the password for the PostgreSQL user that you had set during the installation process.
  5. Then click the OK button. You will now see the pgAdmin dashboard.

This completes the installation of PostgreSQL for the macOS. The next section will familiarize you with the PostgreSQL interface.

Exploring pgAdmin Dashboard

Once you have installed PostgreSQL in Windows and macOS, follow these steps to grasp the interface better:

  1. Open pgAdmin4 from Windows/ macOS (in case pgAdmin is not open on your system).
  2. Click on the Servers option on the left:
Figure 0.33: Clicking on Servers to create a database

Figure 0.33: Clicking on Servers to create a database

  1. Right-click on PostgreSQL 14.
  2. Then click on the Create option.
  3. Choose the Database… option to create a new database:
Figure 0.34: Creating a new database

Figure 0.34: Creating a new database

This will open a Create – Database window.

  1. Enter the database name, as TestDatabase.
  2. Select the Owner of the database or leave it as default. For now, just use the Owner as postgres:
Figure 0.35: Selecting the owner of the database

Figure 0.35: Selecting the owner of the database

  1. Then click on the Save button. This will create a database.
  2. Right-click on Databases and choose the Refresh button:
Figure 0.36: Clicking the Refresh… button after right-clicking Databases

Figure 0.36: Clicking the Refresh… button after right-clicking Databases

A database with the name TestDatabase is now displayed within the dashboard:

Figure 0.37: TestDatabase ready for use

Figure 0.37: TestDatabase ready for use

Now your database is ready to be used for Windows and Mac environments.

PostgreSQL Installation on Ubuntu

In this example, you are using Ubuntu 20.04 for installation. Perform the following steps to do so:

  1. In order to install PostgreSQL, open your Ubuntu terminal first.
  2. Make sure to update your repository using the following command:
    $ sudo apt update
  3. Install the PostgreSQL software along with additional packages using the following command (recommended):
    $ sudo apt install postgresql postgresql-contrib

    Note

    To install only PostgreSQL (not recommended without additional packages), use the command $ sudo apt install postgresql and then press Enter.

This installation process creates a user account called postgres that has the default Postgres role.

Accessing the postgres User Account with the postgres Role

There are two ways to start the PostgreSQL CLI using the postgres user account:

Option 1 is as follows:

  1. To log in as a postgres user, use the following command:
    $ sudo -i -u postgres
  2. Access the CLI by using the following command:
    $ psql

    Note

    Sometimes, while executing the preceding command, a psql error may be displayed as could not connect to server: No such file or directory. This is because of a port issue on your system. Due to this port blockage, the PostgreSQL application may not work. You can try the command again after some time.

  3. To quit the CLI, use the following command:
    $ \q

Option 2 is as follows:

  1. To log in as a postgres user, use the following command:
    $ sudo -u postgres psql
  2. To quit the CLI, use the following command:
    $ \q

Verifying the postgres User Account as a postgres User Role

  1. To verify the user account, log in and use the conninfo command:
    $ sudo -u postgres psql
    $ \conninfo
    $ \q

Using this command, you can ensure that you are connected to the postgres database as the postgres user via port 5432. If you don't want to use the default user, postgres, you can create a new user for you.

Accessing a New User and Database

  1. Create a new user by using the following command and pressing Enter:
    $ sudo -u postgres createuser –interactive

The preceding command will ask the user to add the name of the role and its type.

  1. Enter the name of the role, for example, testUser.
  2. Next, enter y when prompted to set a new role to be a superuser:
    Prompt:
    Enter the name of the role to add: testUser
    Shall the new role be a superuser? (y/n) y

This will create a new user with the name testUser.

  1. Create a new database with the name testdb using the following command:
    $ sudo -u postgres createdb testdb
  2. Log in to the newly created user account using the following command:
    $ sudo -u testUser psql -d testdb
  3. Use the following command to check the connection details:
    $ \conninfo
  4. To quit the CLI, use the following command:
    $ \q

Using this command, you can ensure that you are connected to the testdb database as the testUser user via port 5432.

With these steps, you have completed the PostgreSQL installation for Ubuntu.