Using SqlClient in UWP

By | September 2, 2017

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.

5 thoughts on “Using SqlClient in UWP

  1. William Homer

    The full release of Fall Creators Update doesn’t recognize SqlConnection. I also cannot successfully add a reference to System.Data.SqlClient. What am I missing here?

    Reply
    1. PieEatingNinjas Post author

      Hi William,

      I didn’t try out the release version yet. Will try it asap and will let you know. I wouldn’t expect any issues on this to be honest, but I’ll post my findings here as soon as I’ve tested it on the release version.

      Pieter

      Reply
  2. Harshad Rathjod

    Hi Pieter,

    I installed Windows Fall Creator Update and Visual Studio 2017 15.5. I too am not seeing any option to sue SQLClient. System.Data has only Common lib and there is nothing in references either. Not only that but Windows Template Studio too stopped working.

    Look forward to your findings.

    Thanks
    Harshad

    Reply
    1. Harshad Rathjod

      Quick Update:
      When I upgraded VS2017 to 15.5, I did not realize that it actually installed parallel to old install. I invoked VS using my pinned shortcut (ie old version of VS) and hence it did not support SqlClient. I then found out that there is VS2017 15.5 PREVIEW. Started that and it DOES support SqlClient. I have not tested it further though. But at least SqlClient is available in UWP now.

      Reply
  3. Harshad Rathjod

    And it works with only those projects that do not use any framework or template – eg MVVM Light or any template from Windows Template Studio.

    Reply

Leave a Reply