How To Get Data From On Premise SQL Server Database In PowerApps

Microsoft PowerApps has emerged as one of the fastest app development platforms. You can easily create a native app which can run on any device, like Android/iOS/Windows mobile phones or any desktop browser.

Microsoft has provided tons of connectors to connect to your required data source. Today, we will explore an option to pull data from On-premise SQL Server Database.

There could be two different requirements to pull the data from SQL DB.

  1. You might need all the records from table or view as is – this is directly possible in PowerApps using SQL Connector.
  2. You might need to run some SQL queries or execute a stored procedure and get a set of records in return – this is not possible directly in PowerApps. To achieve this, we need to create a FLOW which will indeed call a stored procedure and return a result to PowerApps.

Prerequisite

We need to install an On-premise Data Gateway on any server, say Server1, in your on-premise network. Make sure your on-premise SQL database is accessible from Server1. Download the latest version of gateway and install it.
 

Let's start with PowerApps. Open your native app or SharePoint customized forms. From the View menu, select Data Sources >> Add data source >> SQL server.

How To Get Data From On Premise SQL Server Database In PowerApps

It will ask whether you want to connect to cloud SQL Server or your on-premise SQL Server.

Select "Connect using on-premise data gateway".
Mention the server name, database name - select either Windows authentication or basic authentication whichever is possible.
Select the Gateway which you have installed as mentioned above.
 

As part of the first requirement where you might need all the records from a table, once the connection is added, you can easily select any table from the database, as shown below.

Now, you can easily bind this table data to the combo box, dropdown list, or Gallery item. It works the same as SharePoint List data source. Now, you can use the filter, sort, and select columns on this data source.

How To Get Data From On Premise SQL Server Database In PowerApps

To fulfill the second requirement, we need to create a FLOW.

Login to https://flow.microsoft.com

Click on My Flows >> Create New Flow from blank
How To Get Data From On Premise SQL Server Database In PowerApps

Add trigger as PowerApps. We will be calling this FLOW from PowerApps.

How To Get Data From On Premise SQL Server Database In PowerApps

Add a new step >> Search for SQL Server. Select “Execute stored procedure” action.

We need to set up a new connection. If the connection is already added, then it will take it automatically. If not, we need to set up the new one. Click on the three-dot ellipsis icon >> Add new connection (or choose existing).
 
How To Get Data From On Premise SQL Server Database In PowerApps
 

Similar to the above step, select the Database name, server name, authentication type, and credentials. Select your installed Gateway.

Once you add the connection and select it, it will directly ask you to choose the stored procedure name from that database.

How To Get Data From On Premise SQL Server Database In PowerApps

Select the required stored procedure. Within the next second, it will load the UI to specify the parameter value (if your stored procedure needs a parameter).

How To Get Data From On Premise SQL Server Database In PowerApps 

We can get this parameter dynamically from PowerApps too. For that, set the value of the parameter as “Ask in PowerApps”, as shown below.

How To Get Data From On Premise SQL Server Database In PowerApps
 
How To Get Data From On Premise SQL Server Database In PowerApps 

Add next action/step as Request/Response. This is needed to respond to the result to PowerApps from where this FLOW will be triggered.

How To Get Data From On Premise SQL Server Database In PowerApps
 
How To Get Data From On Premise SQL Server Database In PowerApps

Save the FLOW and test with a manual trigger. This is needed to get a test response from the SQL database stored procedure.

How To Get Data From On Premise SQL Server Database In PowerApps

After successfully running FLOW, you will see something like below.

How To Get Data From On Premise SQL Server Database In PowerApps

Click on “Execute stored procedure” to see the response.

How To Get Data From On Premise SQL Server Database In PowerApps

Copy the result after [ till ]. - Edit the flow again - Edit Response action - Click on “Use sample payload to generate schema” - Paste the copied result from the above response.

How To Get Data From On Premise SQL Server Database In PowerApps
 
How To Get Data From On Premise SQL Server Database In PowerApps

In the body field of the above action, paste the below code.

body('Execute_stored_procedure')['resultsets']['Table1']

Replace the highlighted text with the name of stored procedure action.

How To Get Data From On Premise SQL Server Database In PowerApps 

Let's go back to PowerApps now. Open your native app in Edit mode or customized forms. Select the button or screen on the event of which you want to trigger the flow. In my case, it's button onclick event.

From Action menu, select FLOW. Look for the FLOW which you have recently created and click on it. This will add the FLOW connection in the app.

Below is how you can call the FLOW to run. Specify the parameter if any.

How To Get Data From On Premise SQL Server Database In PowerApps

From any button, OnSelect in your app, Screen OnVisible event, or App OnStart event, you can write code as shown below to trigger the FLOW.

How To Get Data From On Premise SQL Server Database In PowerApps

Collect the result coming from FLOW into a collection.

How To Get Data From On Premise SQL Server Database In PowerApps

So, you can easily fetch the data from SQL DB from your on-premise server. You can easily execute a stored procedure in your SQL database from PowerApps. That’s it for now.

Thank you.


Similar Articles