.NET Interactive with SQL!| .NET Notebooks in Visual Studio Code
***Please note this post is outdated. The .NET Interactive Notebooks Extension is now named Polyglot Notebooks. See here to learn more: https://devblogs.microsoft.com/dotnet/dotnet-interactive-notebooks-is-now-polyglot-notebooks/ ***
The .NET Interactive team has collaborated with the Azure Data Studio team to bring SQL integration to .NET Notebooks. SQL notebooks combine the benefits of querying data with the rich visualization of notebooks.
First, you’ll need a Microsoft SQL Server or Azure SQL database. If you already have a SQL database, you can use that. If not, you can follow along with the examples below by installing the following:
- SQL Server Developer edition 2019
- A database – In this blog post, I will be using the Adventure Works 2019 lightweight edition. You’ll also need:
- Visual Studio Code
- The .NET Interactive extension
Creating a new SQL Connection in VS Code
Once you have the requirements listed above installed, you are ready to start using SQL in .NET Interactive Notebooks in VS Code.
To create a new notebook, open the Command Palette(
Ctrl+Shift+P on Windows or
Cmd-Shift-P on macOS), and select
.NET Interactive: Create new blank notebook. You can also create a new notebook with
Ctrl+Shift+Alt+N key combination.
Every notebook has a default language. In our case, it’s
C# (.NET Interactive), and we will need to switch the language to
SQL (.NET Interactive). To change the language in a cell, you can either use a magic command in Jupyter (e.g.
#!sql) or the VS Code notebook language selector. The language selector is in the lower right corner of the cell. Click on it and pick
SQL (.NET Interactive) from the language list.
Test the SQL cell out by writing a simple SQL select statement;
SELECT * FROM table_name and run the cell.
Once the cell execution is complete, a list of instructions appears on connecting to a SQL database.
Connecting to a SQL database
The SQL language support in .NET Interactive is added by installing the
Microsoft.DotNet.Interactive.SqlServer package. We can do this by adding a new
C# (.NET Interactive) cell and running(Click on +Code to add a new cell):
To learn more about connecting to a Microsoft SQL Server Database, execute the help command in a new cell.
#!connect mssql -h
Now that we have all the prerequisites listed above, we can connect to database. To establish a connection, you will need a
--kernel-name and the connection string to the database.
In a new
C# (.NET Interactive) cell, establish a connection and label it
adventureworks and add the connection string. Once this cell is executed the
#!sql-adventureworks sub- kernel is available for use.
We have our database connected so, let’s create a new SQL(.NET Interactive) cell and write a select query that would grab all the Adventure Works database data. Enter this
SELECT * FROM AdventureWorksLT2019 and execute the cell.
After the cell runs, you will notice an info message in the available connections in the output. In this case, we only have the #!sql-adventureworks sub-kernel, but you can connect have multiple databases in a single notebook.
Querying and Visualizing Data
In a new
SQL (.NET Interactive) cell, you can now specify your connection using
#!sql-adventureworks magic command followed by a query against the AdventureWorks database. You will notice in the image below that since we have specified the SQL sub-kernel, we get code completion on the database tables and columns!
Once you have completed writing your SQL query,run the cell. The results of the query are displayed using the nteract Data Explorer,providing a rich interactive filter and visualization experience where the user can explore their data in a number of ways.
- Filter and sort by column – Click on Show Filters
- Using the toolbar, select a preferred visualization:
Entity Framework Core in a SQL Notebook
EF Core is the data access API of choice for .NET developers. Bringing EF Core and SQL together in .NET Interactive Notebooks provides users the quick iteration of notebooks while providing strongly typed APIs, code completion over tables and columns, and Language-Integrated-Query (LINQ). Quickly explore your data’s shape and schema, craft queries with filtering, sorting, aggregations, and projections that seamlessly translate to SQL, and return results for analysis and visualization.
As an example, I am going to demo how you can start leveraging EF Core and quickly. I will demonstrate how you can use
--create-dbcontext option to scaffold a
DbContext that you can access in the C# cell.
Start by adding a new
C#(.NET Interactive) cell and establish a new connection to your database label it
adventureworksEF. In addition to giving a new
--kernel-name, you will need the include the
Your new connection should look like the code snippet below; once you are done, run the cell.
The cell above does the following:
- Scaffolds a
DbContextand initializes an instance of it called
adventureworksEFin the C# kernel.
- Installs the
- Adds a new sub-kernel
In a new
C# (.NET Interactive) cell, you can now start exploring and querying your data using the
adventureworksEF DbContext. You will notice in the image that we continue to get code completion over tables and columns and get visualizations of our data. The visualizations are generated through
dataFrame.Explore() from the nteract data explorer.
Now, since we know the shape of our data, I can write a LINQ to query all the data in my table of a particular order number. For example
var qty = adventureworksEF.SalesOrderDetail.Where(q => q.OrderQty.Equals(4)).ToList();
And there you have it! A simple demonstration on how you can leverage .NET Interactive notebooks with SQL and EF Core.
- Source code
- .NET Interactive Notebook Extension
- Azure Data Studio
- Introduction to LINQ
Happy interactive programming!