I\'m trying to connect to an on-prem MS SQL database from a universal windows app. I\'m making a LOB app using UWP, to support desktop, tablet and mobile use. When trying to
I am having to go down this same road as well... Looking forward to SQLServer being directly accessible via EF Core directly.
I have watched both tutorials above and since I'm new to developing, it only wet my appetite. I did however find this detailed Video Tutorial on YouTube that walks you thru;
Despite this Video NOT being in English, I was able to watch what he was doing then pause and duplicate.
Here is a simple sample and a video. Not sure if it's enough for you.
Here is a difficult point is
Note: From windows 10 Fall Creators Update (16299) we can directly access SQL Server database using .NetStanded 2.0
As there is no direct way to connect to SQL Server, we need to create an API for our database in order to connect to SQL Server.
This solution describes
Launch Visual Studio Installer and click Modify
Install ASP.NET and web development
Add new project in your solution
Select ASP.NET Web Application (.Net Framework) and give a project name
Select Web API and click OK
Add new item in Models folder
Select ADO.NET Entity Data Model and give it a name
Select EF Designer from database and click Next
Click New Connection
Configure your connection, click OK and click Next
Select Entity Framework version and click next
Select Databases and Tables to be connected and Click Finish
Rebuild your project before doing forther
Add new Controller in Controllers folder
Select Web API 2 Controller with actions, using Entity Framework and click Add
Select Model class (table name) and Data context class (database name) from the drop-down list box and click Add
Set this project as the startup project
Run the project in a web browser
Now your browser will open a localhost site. Click API in top
This page shows all the API available from your project
Copy any API link from below and replace it with the "Help" in URI and press Enter. Now you should able to see your data from the SQL Server database
HttpClient httpClient = new HttpClient();
var jsonReponse = await httpClient.GetStringAsync("http://localhost:xxxxx/api/LogIns");
logInResult = JsonConvert.DeserializeObject<List<LogIn>>(jsonReponse);
You can get the model class from Models
Just create the same class in your UWP project
var logIn = new Models.LogIn()
{
Username = "username",
Password = "password"
};
var logInJson = JsonConvert.SerializeObject(logIn);
HttpClient httpClient = new HttpClient();
var httpContent = new StringContent(logInJson);
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
await httpClient.PostAsync("http://localhost:56267/api/LogIns", httpContent);
For more info about JSON Serialization And Deserialization Using JSON.NET Library In C#
With the Windows 10 Fall Creators Update (build 16299) UWP apps can now access SQL Server directly via the standard NET classes (System.Data.SqlClient) - thanks to the newly added support for .NET Standard 2.0 in UWP.
Here is a Northwind UWP demo app: https://github.com/StefanWickDev/IgniteDemos
We have presented this demo at Microsoft Ignite in September 2017, here is the recording of our session (skip to 23:00 for the SQL demo): https://myignite.microsoft.com/sessions/53541
Here is the code to retrieve the products from the Northwind database (see DataHelper.cs in the demo). Note that it is exactly the same code that you would write for a Winforms or WPF app - thanks to the .NET Standard 2.0:
public static ProductList GetProducts(string connectionString)
{
const string GetProductsQuery = "select ProductID, ProductName, QuantityPerUnit," +
" UnitPrice, UnitsInStock, Products.CategoryID " +
" from Products inner join Categories on Products.CategoryID = Categories.CategoryID " +
" where Discontinued = 0";
var products = new ProductList();
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
if (conn.State == System.Data.ConnectionState.Open)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = GetProductsQuery;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var product = new Product();
product.ProductID = reader.GetInt32(0);
product.ProductName = reader.GetString(1);
product.QuantityPerUnit = reader.GetString(2);
product.UnitPrice = reader.GetDecimal(3);
product.UnitsInStock = reader.GetInt16(4);
product.CategoryId = reader.GetInt32(5);
products.Add(product);
}
}
}
}
}
return products;
}
catch (Exception eSql)
{
Debug.WriteLine("Exception: " + eSql.Message);
}
return null;
}
If you need to support earlier versions than the Fall Creators Update, there is also a way for you to call SqlClient APIs from your UWP app package, via the Desktop Bridge. I have a sample for this published here: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/SQLServer