Forum Discussion
Connecting to Azure SQL Server from a Fabric Compute Notebook using Managed Identity
- Anonymous1 year ago
Hi hkaushik ,
I want to personally thank nilendraFabric for your active participation and the valuable solutions you have shared in the community forum. Your contributions truly make a significant impact.
Thank you for trying the provided approach. Since it did not resolve the issue, I kindly recommend raising a support ticket. This will enable a dedicated team to investigate and provide you with a resolution.To raise a support ticket for Fabric and Power BI, kindly follow the steps outlined in the following guide:
How to create a Fabric and Power BI Support ticket - Power BI | Microsoft Learn
If this post helps, please give us Kudos and consider marking it Accept as solution to assist other members in finding it more easily.
Thank you for being a part of Microsoft Fabric Community Forum!
Thanks,
Pallavi G.
Did you tried this approach
Instead of pyodbc, use the Spark JDBC connector, which better integrates with Fabric environments:
jdbc_url = f"jdbc:sqlserver://{server_name}.database.windows.net:1433;database={database_name};encrypt=true;trustServerCertificate=false;Authentication=ActiveDirectoryMSI"
query = "(SELECT * FROM your_table) AS temp"
df = spark.read \
.format("com.microsoft.sqlserver.jdbc.spark") \
.option("url", jdbc_url) \
.option("dbtable", query) \
.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
.load()
df.show()
====================
If you must use pyodbc, manually retrieve an access token for Workspace Identity:
from notebookutils import mssparkutils
# Get token for Azure SQL
token = mssparkutils.credentials.getToken(audience="https://database.windows.net/")
# Build connection string
connection_string = (
"Driver={ODBC Driver 18 for SQL Server};"
f"Server={server_name};"
f"Database={database_name};"
"Encrypt=yes;TrustServerCertificate=yes;"
)
# Connect using pyodbc
import pyodbc
conn = pyodbc.connect(connection_string, attrs_before={1256: token})
Had tried both the approaches already.
JDBC gives ManagedId related error:
PYODBC approach, the token retrieved there is my personal token and not the workspace's.