How to connect to SQL server database from a Windows 10 UWP app

前端 未结 4 1457
闹比i
闹比i 2020-11-27 05:57

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

4条回答
  •  猫巷女王i
    2020-11-27 06:46

    Connecting UWP to SQL Server

    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

    1. Creating API
    2. Serialize and Deserialize JSON data

    1. Creating API

    1) Installing ASP.NET and web development

    1. Launch Visual Studio Installer and click Modify

    2. Install ASP.NET and web development

    2) Creating new ASP.NET Web Application (.Net Framework)

    1. Add new project in your solution

    2. Select ASP.NET Web Application (.Net Framework) and give a project name

    3. Select Web API and click OK

    3) Connecting to SQL Server database

    1. Add new item in Models folder

    2. Select ADO.NET Entity Data Model and give it a name

    3. Select EF Designer from database and click Next

    4. Click New Connection

    5. Configure your connection, click OK and click Next

    6. Select Entity Framework version and click next

    7. Select Databases and Tables to be connected and Click Finish

    4) Add Controllers to communicate with models

    1. Rebuild your project before doing forther

    2. Add new Controller in Controllers folder

    3. Select Web API 2 Controller with actions, using Entity Framework and click Add

    4. Select Model class (table name) and Data context class (database name) from the drop-down list box and click Add

    5) Testing API

    1. Set this project as the startup project

    2. Run the project in a web browser

    3. Now your browser will open a localhost site. Click API in top

    4. This page shows all the API available from your project

    5. 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

    2. Serialize and Deserialize JSON data

    1) Install Newtonsoft.Json

    2) Deserializing JSON

    HttpClient httpClient = new HttpClient();
    var jsonReponse = await httpClient.GetStringAsync("http://localhost:xxxxx/api/LogIns");
    logInResult = JsonConvert.DeserializeObject>(jsonReponse);
    

    You can get the model class from Models

    Just create the same class in your UWP project

    3) Serializing JSON

    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#

提交回复
热议问题