-
Book Overview & Buying
-
Table Of Contents
Apps and Services with .NET 7
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.
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.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 the following table:
|
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 |
|
Good Practice: Use a dot . as shorthand for the local computer name. 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 Setting up Azure SQL Database or Installing Azure SQL Edge in Docker. Be sure to read the Creating the Northwind sample database section to learn where to find the SQL scripts that create the sample database.
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.
To download and configure SQL Server Developer Edition, use the following steps:
net7book.The direct link to download SSMS is as follows: https://docs.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.2:

Figure 2.2: SQL Server (mssql) extension for Visual Studio Code
Now we can run a database script to create the Northwind sample database on Windows using SQL Server Management Studio (SSMS):
apps-services-net7 folder, create a folder named Chapter02./sql-scripts/Northwind4SQLServer.sql into the Chapter02 folder.. (a dot), meaning the local computer name, and then click Connect.
If you had to create a named instance, like net7book, then enter .\net7book.
Northwind4SQLServer.sql file and then click Open.
Figure 2.3: 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 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.
apps-services-net7 and select a suitable region close to you, and then click the Review + create button.Northwind, and select the resource group that you created before.apps-services-net7-[your initials] or something else entirely. The server name must be globally unique because it becomes part of a public URL.markjprice.
Figure 2.4: Entering the server details for a SQL Database instance

Figure 2.5: Deployment progress for SQL Database

Figure 2.6: SQL Database details
Server=tcp:apps-services-net7.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-net7, is public and must be globally unique.

Figure 2.7: Connecting to your Azure SQL database from Visual Studio
In Visual Studio Server Explorer, you might also be prompted to Choose Data Source. Choose Microsoft SQL Server. You can select a checkbox to always use this selection.
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-net7/tree/main/sql-scripts.
You now have a running Azure SQL database that you can connect to from a .NET project.
If you do not have a Windows computer, and you do not want to pay for Azure resources, then you can install Docker and use a container that has Azure SQL Edge, a cross-platform minimal featured version of SQL Server that only includes the database engine.
The Docker image we will use has Azure SQL Edge based on Ubuntu 18.4. It is supported with the Docker Engine 1.8 or later on Linux, or on Docker for Mac or Windows. Azure SQL Edge requires a 64-bit processor (either x64 or ARM64), with a minimum of one processor and 1 GB RAM on the host.
docker pull mcr.microsoft.com/azure-sql-edge:latest
latest: Pulling from azure-sql-edge
2f94e549220a: Pull complete
830b1adc1e72: Pull complete
f6caea6b4bd2: Pull complete
ef3b33eb5a27: Pull complete
8a42011e5477: Pull complete
f173534aa1e4: Pull complete
6c1894e17f11: Pull complete
a81c43e790ea: Pull complete
c3982946560a: Pull complete
25f31208d245: Pull complete
Digest: sha256:7c203ad8b240ef3bff81ca9794f31936c9b864cc165dd187c23c5bfe06cf0340
Status: Downloaded newer image for mcr.microsoft.com/azure-sql-edge:latest
mcr.microsoft.com/azure-sql-edge:latest
azuresqledge, as shown in the following command:
docker run --cap-add SYS_PTRACE -e 'ACCEPT_EULA=1' -e 'MSSQL_SA_PASSWORD=s3cret-Ninja' -p 1433:1433 --name azuresqledge -d mcr.microsoft.com/azure-sql-edge
Good Practice: The password must be at least 8 characters long and contain characters from three of the following four sets: uppercase letters, lowercase letters, digits, and symbols; otherwise, the container cannot set up the SQL Edge engine and will stop working.

Figure 2.8: SQL Edge running in Docker Desktop on Windows
docker ps -a
1433, which is mapped to its internal port 1433, as shown highlighted in the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
183f02e84b2a mcr.microsoft.com/azure-sql-edge "/opt/mssql/bin/perm…" 8 minutes ago Up 8 minutes 1401/tcp, 0.0.0.0:1433->1433/tcp azuresqledge
You can learn more about the docker ps command at the following link: https://docs.docker.com/engine/reference/commandline/ps/.

Figure 2.9: Connecting to your Azure SQL Edge server from Visual Studio
Northwind4AzureSQLedge.sql file into the query window and execute it.You now have a running instance of Azure SQL Edge containing the Northwind database that you can connect to from a console app.
Change the font size
Change margin width
Change background colour