Hello .NET Standard 2.0

A week or so ago, Visual Studio Preview 15.4 released and I was pretty excited about this! Why? Well, Visual Studio 2017 version 15.4 Preview brings first class support for developers targeting the Windows Fall Creators Update SDK preview. This Windows Fall Creators Update brings support for .NET Standard 2.0 to UWP which means we now have a set of 19k additional APIs we can acces (compared to .NET Standard 1.6).

And personally, I’m most excited about the SqlClient APIs.

SqlClient & UWP?

Yes! We now have access to the APIs that live in the System.Data.SqlClient namespace, meaning we can now access Sqlserver databases directly from within our UWP app. This is yet another step in the direction of making UWP a good fit for LOB applications. Countless LOB applications have been written in the past that use Sqlserver Express on the client to store data locally.

Show me how

At the moment of writing, the Windows Fall Creators Update SDK is still in preview, so you have to download it from here. The same thing goes for Visual Studio 15.4 which you can find here.

First, create a new UWP project and select Windows 10 Insider Preview (10.0; Build 16267) as Target Version and Minimum Version.
Minimum and Target VersionOnce this is done, we can use the SqlClient APIs just like we’ve always been doing in WPF or WinForms.

And what better way to demo this using the Northwind DB? 🙂

But first, some UI

A very simple UI with a TextBox, a search Button  and a ListView  to show the results. Also provide a loading message while the app is querying the database.

Now, query all the things

For this demo I’ve built a Northwind repository with just one method: Search. In this method we are just using the SqlClient APIs that we know. We define our query, make a connection to the DB, pass in the query parameter  in the SqlCommand and finally execute the command. Once we get the result back, we make an instance of a Product for each line and return them.

In the above code, you’ll notice that I’ve surrounded it all with Task.Run. I do this because the SqlClient APIs aren’t async. But methods like ExecuteReader  can take long to process. In order to keep our UI responsive, I’ve put this all inside a Task  so I can use the async/await  pattern.

That’s all, folks

Aaah, we can finally -with UWP implementing .NET Standard 2.0- use the SqlClient APIs (and others). This really opens up the potential of UWP for LOB app even more! I like it!

You can find the code of this blogpost on my GitHub.