Link to home
Start Free TrialLog in
Avatar of Star79
Star79Flag for United States of America

asked on

How to call PaaS SQL server stored procedure from Azure Functions in Azure

I am able to calling PaaS SQL server stored procedure from logic app but from Azure funcitons i am having a issue.  Can you please help.
Avatar of lcohan
lcohan
Flag of Canada image

To Call a SQL stored procedure from the Azure Function using the below steps:
  • Log in to the Azure Portal
  • Create an Azure Function App in Visual Studio 2019 and use the SQL Database connection string from Azure Portal, SQLConnection, and SQLCommand object to call the Stored Procedure.
Essentially the code to call/exec the SP would be something like:

using System.Net;
using System.Data;
using System.Data.SqlClient;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function stored procedure call request.");

    dynamic body = await req.Content.ReadAsStringAsync();
    
    //Connect to SQL
    var cnnString  = "XXXXX";
        
    using (SqlConnection conn = new SqlConnection(cnnString))
    {
        conn.Open();
        
        SqlCommand cmd = new SqlCommand("YourSPnameHere", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        
        var reader = cmd.ExecuteReader();
    }

    return req.CreateResponse(HttpStatusCode.OK, "Ok");
}

Open in new window

For more details/screenshots please have a look at this link: https://azurelessons.com/call-a-stored-procedure-from-azure-functions/
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.