-
Book Overview & Buying
-
Table Of Contents
Apps and Services with .NET 8 - Second Edition
By :
Two of the most common places to store data are in a Relational Database Management System (RDBMS) such as SQL Server, PostgreSQL, MySQL, and SQLite, or in a NoSQL database such as Azure Cosmos DB, MongoDB, Redis, and Apache Cassandra.
In this chapter, we will focus on the most popular RDBMS for Windows, which is SQL Server. This product is also available in a version for Linux. For cross-platform development, you can use either Azure SQL Database, which stores the data in the cloud, or Azure SQL Edge, which can run in a Docker container on Windows, macOS, or Linux, on both Intel and ARM architecture CPUs.
To learn how to manage an RDBMS using .NET, it would be useful to have a sample one so that you can practice on a database that has a medium complexity and a decent number of sample records.
Microsoft offers several sample databases, most of which are too complex for our needs, so instead, we will use a database that was first created in the early 1990s known as Northwind.
Let’s take a minute to look at a diagram of the Northwind database and its eight most important tables. You can use the diagram in Figure 2.1 to refer to as we write code and queries throughout this book:

Figure 2.1: The Northwind database tables and relationships
Note that:
Categories and Products is one-to-many, meaning each category can have zero, one, or more products.ReportsTo field is null), and a photo stored as a byte array in JPEG format. The table has a one-to-many relationship to itself because one employee can manage many other employees.To connect to a SQL Server database, we need to know multiple pieces of information, as shown in the following list:
We specify this information in a connection string.
For backward compatibility, there are multiple possible keywords we can use in a SQL Server connection string for the various parameters, as shown in the following list:
Data Source, server, or addr: These keywords are the name of the server (and an optional instance). You can use a dot (.) to mean the local server.Initial Catalog or database: These keywords are the name of the database that will be active initially. A SQL statement could change that using the command: USE <databasename>.Integrated Security or trusted_connection: These keywords are set to true or SSPI to pass the thread’s current user credentials using Windows Authentication.User Id and Password: These keywords are used to authenticate with any edition of SQL Server. This is important for Azure SQL Database or Azure SQL Edge because they do not support Windows Authentication. The full edition of SQL Server on Windows supports both username with password and Windows Authentication.Authentication: This keyword is used to authenticate by using Azure AD identities that can enable password-less authentication. Values can be Active Directory Integrated, Active Directory Password, and Sql Password.Persist Security Info: If set to false, this keyword tells the connection to remove the Password from the connection string after authenticating.Encrypt: If set to true, this keyword tells the connections to use SSL to encrypt transmissions between client and server.TrustServerCertificate: Set to true if hosting locally and you get the error “A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)”.Connection Timeout: This keyword defaults to 30 seconds.MultipleActiveResultSets: This keyword is set to true to enable a single connection to be used to work with multiple tables simultaneously to improve efficiency. It is used for lazy loading rows from related tables.As described in the list above, when you write code to connect to a SQL Server database, you need to know its server name. The server name depends on the edition and version of SQL Server that you will connect to, as shown in Table 2.1:
|
SQL Server edition |
Server name \ Instance name |
|
LocalDB 2012 |
|
|
LocalDB 2016 or later |
|
|
Express |
|
|
Full/Developer (default instance) |
|
|
Full/Developer (named instance) |
|
|
Azure SQL Edge (local Docker) |
|
|
Azure SQL Database |
|
Table 2.1: Server name examples for various editions of SQL Server
Good Practice: Use a dot (.) as shorthand for the local computer name (localhost). Remember that server names for SQL Server can be made up of two parts: the name of the computer and the name of a SQL Server instance. You provide instance names during custom installation.
Microsoft offers various editions of its popular and capable SQL Server product for Windows, Linux, and Docker containers. If you have Windows, then you can use a free version that runs standalone, known as SQL Server Developer Edition. You can also use the Express edition or the free SQL Server LocalDB edition that can be installed with Visual Studio 2022 for Windows.
If you do not have a Windows computer or if you want to use a cross-platform database system, then you can skip ahead to the topic Setting up Azure SQL Database, or the online-only section Installing Azure SQL Edge in Docker found at the following link:
https://github.com/markjprice/apps-services-net8/blob/main/docs/ch02-sql-edge.md
If you prefer to install SQL Server locally on Linux, then you will find instructions at the following link: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-setup.
On Windows, if you want to use the full edition of SQL Server instead of the simplified LocalDB or Express editions, then you can find all SQL Server editions at the following link: https://www.microsoft.com/en-us/sql-server/sql-server-downloads.
Take the following steps:

Figure 2.2: Installing a new instance of SQL Server
apps-services-book.
Figure 2.3: Downloading SQL Server Management Studio (SSMS)
The direct link to download SSMS is as follows: https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms.
Azure Data Studio (ADS) is automatically installed alongside SSMS. ADS is cross-platform and open-source, so you can use it to work with SQL Server databases on any desktop operating system.
There are many tools that make it easy to work with SQL Server. If you are using Visual Studio Code, then you can install the SQL Server (mssql) ms-mssql.mssql extension. If you install the extension, it adds a new view to the Primary Side Bar titled SQL Server, as shown in Figure 2.4:

Figure 2.4: SQL Server (mssql) extension for Visual Studio Code
Now we can run a database script to create the Northwind sample database locally on Windows using SQL Server Management Studio (SSMS):
apps-services-net8 folder, create a folder named Chapter02./scripts/sql-scripts/Northwind4SQLServer.sql into the Chapter02 folder.. (a dot), meaning the local computer name, and then click Connect.Warning! If you had to create a named instance, like apps-services-book, then enter .\apps-services-book. If you see an error about the server certificate, then click the Options >> button and select the Trust server certificate check box.
Northwind4SQLServer.sql file and then click Open.
Figure 2.5: The Products table in SQL Server Management Studio
We did not have to use SQL Server Management Studio to execute the database script. We can also use tools in Visual Studio 2022, including the SQL Server Object Explorer and Server Explorer, or cross-platform tools like the Visual Studio Code extension for SQL Server, or Azure Data Studio, which you can download and install separately from the following link: https://aka.ms/getazuredatastudio.
If you do not have a Windows computer, then you can create a cloud-hosted instance of SQL Server. You will need an Azure account. You can sign up at the following link: https://signup.azure.com. Next, you need to take the following steps:
apps-services-book and select a suitable region close to you, and then click the Review + create button, as shown in Figure 2.6:
Figure 2.6: Creating a resource group in the Azure portal
Northwind, and select the resource group that you created before.apps-services-book-[your initials] or something else entirely. The server name must be globally unique because it becomes part of a public URL.markjprice.
Figure 2.7: Entering the server details for a SQL Database instance

Figure 2.8: Deployment progress for SQL Database

Figure 2.9: SQL Database details
Server=tcp:apps-services-book.database.windows.net,1433;
Initial Catalog=Northwind;
Persist Security Info=False;
User ID=markjprice;
Password={your_password};
MultipleActiveResultSets=False;
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=30;
Your Server value will be different because the custom server name part, for example, apps-services-book, is public and must be globally unique.
If you use JetBrains Rider on any operating system, then you can use the following steps to connect with a SQL Server database:
{your_password} to the password you chose.Now we can run a database script to create the Northwind sample database in the Azure SQL Database:

Figure 2.10: Connecting to your Azure SQL database from Visual Studio
You might also be prompted to Choose Data Source. Choose Microsoft SQL Server. You can select a checkbox to always use this selection.
If you are using JetBrains Rider, then right-click the SQL Server, in the popup menu, select SQL Scripts | Run SQL Script…, and then select the Northwind4AzureSQLdatabase.sql file.
Northwind4AzureSQLdatabase.sql file into the query window and execute it.The main difference between the Northwind4SQLServer.sql and Northwind4AzureSQLdatabase.sql scripts is that the local SQL Server script will delete and recreate the Northwind database. The Azure SQL database script will not because the database needs to be created as an Azure resource. You can download SQL script files from the following link: https://github.com/markjprice/apps-services-net8/tree/main/scripts/sql-scripts.
You now have a running Azure SQL database in the cloud that you can connect to from a .NET project.
Change the font size
Change margin width
Change background colour