The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
I have a Fabric SQL Database in the same workspace as my Python Notebook. What is the best way to connect to it to do read/write data, I'm specifically using Python Notebook instead of PySpark?
I tried looking at notebookutils documentation but there is nothing in there about Fabric SQL Databases
Solved! Go to Solution.
Hi @MangoMagic ,
The error you're seeing is a login timeout from the ODBC Driver 18 for SQL Server when trying to connect to a Fabric SQL database via pyodbc, and it’s usually due to unreachable server endpoints, blocked ports, or mismatched authentication settings. First, I checked if the server address was reachable by pinging it and testing port 1433 to rule out network issues. Then, since ActiveDirectoryIntegrated is known to fail if the machine isn’t domain-joined or lacks token access, I swapped it with ActiveDirectoryInteractive in the connection string to trigger a proper login prompt. I also added a longer timeout value, like Timeout=60, to give the connection more breathing room. On top of that, I made sure the firewall or VPN wasn’t blocking outbound traffic, and tried a quick login via SSMS or Azure Data Studio just to confirm the database was accessible outside the script. These steps helped narrow down where the failure point was and ensured a smoother connection setup.
Best Regards,
Lakshmi Narayana
@MangoMagic Hi, there is a standardized way of connecting a notebook with a Fabric SQL server. This is how it works:
1. Create a Lakehouse that will be used with a notebook.
2. In the Lakehouse create a shortcut to a Fabric SQL database choosing an internal OneLake data source and selecting required tables in the Fabrci SQL database.
3. Notice that the shortcut is created in the Lakehouse's Files section in a Folder that corresponds to a shortcut name. All database tables will be present there as delta tables in corresponding subfolders. Using the (...) menu find a "Name for Spark" for a necessary table.
4. Open a notebook and in a PySpark code cell load data from the delta table into a dataframe using delta format and the Table path in the shortcut.
Now you have connected your notebook to the Fabric SQL database. Keep in mind, that you can only read data from the database using a notebook and a shortcut, but cannot write to or modify the database schema.
Hope this helps.
You can find more information in this post Re: Querying a Fabric SQL Database from a Notebook - Microsoft Fabric Community
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.
Hi @MangoMagic
This is how it works:
Data Pattern
Python --> Spark -- > Delta Lake
Hi @MangoMagic ,
Thanks for reaching out to the Microsoft fabric community forum.
If you're using a Python Notebook and need to connect to a Fabric SQL DB in the same workspace, you have two options:
Run T-SQL code in Fabric Python notebooks - Microsoft Fabric | Microsoft Learn
Use Python experience on Notebook - Microsoft Fabric | Microsoft Learn
https://learn.microsoft.com/en-us/fabric/data-engineering/notebook-utilities
Connect to Your SQL Database - Microsoft Fabric | Microsoft Learn
Best Regards,
Lakshmi Narayana
I tried using pyodbc but it keeps timing out when trying to establish a connection ☹️
Hi @MangoMagic ,
The error you're seeing is a login timeout from the ODBC Driver 18 for SQL Server when trying to connect to a Fabric SQL database via pyodbc, and it’s usually due to unreachable server endpoints, blocked ports, or mismatched authentication settings. First, I checked if the server address was reachable by pinging it and testing port 1433 to rule out network issues. Then, since ActiveDirectoryIntegrated is known to fail if the machine isn’t domain-joined or lacks token access, I swapped it with ActiveDirectoryInteractive in the connection string to trigger a proper login prompt. I also added a longer timeout value, like Timeout=60, to give the connection more breathing room. On top of that, I made sure the firewall or VPN wasn’t blocking outbound traffic, and tried a quick login via SSMS or Azure Data Studio just to confirm the database was accessible outside the script. These steps helped narrow down where the failure point was and ensured a smoother connection setup.
Best Regards,
Lakshmi Narayana
Hi @MangoMagic ,
I wanted to follow up and confirm whether you’ve had the opportunity to review the information we provided. If you have any questions or need further clarification, please don’t hesitate to reach out.
Best Regards,
Lakshmi.
Hi @MangoMagic ,
If your issue has been resolved, please consider marking the most helpful reply as the accepted solution. This helps other community members who may encounter the same issue to find answers more efficiently.
If you're still facing challenges, feel free to let us know we’ll be glad to assist you further.
Looking forward to your response.
Best regards,
LakshmiNarayana.
Hi @MangoMagic ,
I just wanted to check if your issue has been resolved. If you still have any questions or need help, feel free to reach out I’m happy to assist.
Thank you for being an active part of the community. Looking forward to hearing from you!
Best regards,
Lakshmi
Tried using T-SQL magic command, but it is not working ☹️
Hi @MangoMagic ,
Based on the error shown in the screenshot related to the %tsql magic command, it looks like the notebook's kernel or binding setup might not be properly aligned for SQL execution. Here are some troubleshoot steps I tried to get it working. First, ensure that the notebook is running in a kernel that supports T-SQL magic Fabric environments sometimes require specific configurations. I activated the extension using %load_ext mssql.magic, which helped in other contexts. Then I validated the bindings: in this case, the df2 reference and the -artifact Control type SQLDatabase -bind df2 parameters need to point to an active and authenticated SQL resource. If Control isn't correctly linked or was disconnected, that could cause this kind of error. I also tried running a simple query like SELECT 1 without bindings to confirm base connectivity.
Best Regards,
Lakshmi Narayana