Code: Select all
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
Code: Select all
import pyodbc
import os
import logging
def main(req: func.HttpRequest) -> func.HttpResponse:
try:
server = os.environ["SQL_SERVER"]
database = os.environ["SQL_DATABASE"]
connection_string = f"Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},1433;Database={database};Authentication=ActiveDirectoryMsi;"
# Attempt to connect (with a longer timeout, which doesn't fix the issue)
with pyodbc.connect(connection_string, timeout=60) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT @@VERSION")
row = cursor.fetchone()
logging.info(f"SQL Version: {row[0]}")
return func.HttpResponse("Successfully connected to the database.", status_code=200)
except Exception as e:
logging. Error(f"Error connecting to database: {e}")
return func.HttpResponse("Failed to connect to the database.", status_code=500)