Connect to a MSSQL database within an Azure function

Kaat Vandecasteele 6 Reputation points
2022-09-15T14:52:13.193+00:00

I have created an Azure function (in Python). Within that function I want to do a query from a MS SQL server. I make a connection with the following code:
server = 'myserver,port_nr'
database = 'Application'
Authentication='ActiveDirectoryMsi'
driver= '{ODBC Driver 17 for SQL Server}'

conn_sleep = pyodbc.connect('DRIVER='+driver+   
                  ';SERVER='+server+  
                  ';PORT=1433;DATABASE='+database+  
                  ';AUTHENTICATION='+Authentication  
                  )  

It gives me this error: Result: Failure Exception: OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)') Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py",

Does someone has a suggestion to solve it?
Kind regards,
Kaat

Azure SQL Database
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,212 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Kaat Vandecasteele 6 Reputation points
    2022-09-26T07:14:58.92+00:00

    Hi Oury,

    Our problem is solved. Thank you very much!

    regards,
    Kaat

    1 person found this answer helpful.

  2. SUNOJ KUMAR YELURU 13,921 Reputation points MVP
    2022-09-15T16:03:46.78+00:00

    Hi @Kaat Vandecasteele

    Connect Azure SQL database from Python Function App using managed identity or access token.
    refer this blog

    ------

    If this answers your query, do click Accept Answer and Up-Vote for the same. And, if you have any further query do let us know.

    0 comments No comments

  3. Oury Ba-MSFT 16,076 Reputation points Microsoft Employee
    2022-09-22T17:14:30.31+00:00

    Hi @Kaat Vandecasteele

    I would suggest following the blog shared by @SUNOJ KUMAR YELURU below. It covers the step by step on how to connect Azure SQL Database from Python function App using managed Identity or access token.

    Please let us know if that helps or shared the steps you took when trying to connect.

    Regards,
    Oury


  4. Kaat Vandecasteele 6 Reputation points
    2022-09-27T14:21:32.487+00:00

    Hi,

    I followed this blog: https://techcommunity.microsoft.com/t5/apps-on-azure-blog/how-to-connect-azure-sql-database-from-python-function-app-using/ba-p/3035595
    using managed identity.
    The main problem that I had is that I was connecting to an SQL database where I was not owner of and my Azure function (on Linux) couldn't make a connection to it. I coudn't solve this problem. I copied a part of the dataset in another SQL database where I am the owner of. I could connect to this database without any problems.

    Kind regards