Forum Discussion
Error connecting to SQL Database using Fabric notebook
- Anonymous2 years ago
Hi Ryan_OC ,
I have tried connecting to it using pyodbc, Can you give a try?
import pyodbc tenant_id = "tenant_id" service_principal_id = f"client_id@{tenant_id}" # important to include your @fully qualified domain or tenant_id service_principal_secret = "client_secret" # Define your SQL Server details server_name = "server_connection_string" database_name = "database_name" queryStr = 'SELECT 1 AS a, 2 AS b UNION ALL SELECT 2 AS a, 3 AS b' # Define the SQL Server ODBC connection string conn_str = ( f"DRIVER={{ODBC Driver 18 for SQL Server}};" f"SERVER={server_name};" f"DATABASE={database_name};" f"UID={service_principal_id};" f"PWD={service_principal_secret};" f"Authentication=ActiveDirectoryServicePrincipal" ) # Establish the connection conn = pyodbc.connect(conn_str) # Execute a query cursor = conn.cursor() cursor.execute(queryStr) resultList = cursor.fetchall() resultColumns = columns = [column[0] for column in cursor.description] print(str([dict(zip(columns, row)) for row in resultList]))
Incase if this doesn't help, I will try to do a deeper investigation.
Thank you
Hi Ryan_OC ,
I have tried connecting to it using pyodbc, Can you give a try?
import pyodbc
tenant_id = "tenant_id"
service_principal_id = f"client_id@{tenant_id}" # important to include your @fully qualified domain or tenant_id
service_principal_secret = "client_secret"
# Define your SQL Server details
server_name = "server_connection_string"
database_name = "database_name"
queryStr = 'SELECT 1 AS a, 2 AS b UNION ALL SELECT 2 AS a, 3 AS b'
# Define the SQL Server ODBC connection string
conn_str = (
f"DRIVER={{ODBC Driver 18 for SQL Server}};"
f"SERVER={server_name};"
f"DATABASE={database_name};"
f"UID={service_principal_id};"
f"PWD={service_principal_secret};"
f"Authentication=ActiveDirectoryServicePrincipal"
)
# Establish the connection
conn = pyodbc.connect(conn_str)
# Execute a query
cursor = conn.cursor()
cursor.execute(queryStr)
resultList = cursor.fetchall()
resultColumns = columns = [column[0] for column in cursor.description]
print(str([dict(zip(columns, row)) for row in resultList]))
Incase if this doesn't help, I will try to do a deeper investigation.
Thank you
OK I gave it a try and I received this:
[{'a': 1, 'b': 2}, {'a': 2, 'b': 3}] and it stated it was successful. Was it successful?
SO now do I need to change the QueryStr to access a table?
- Anonymous2 years agoNot applicable
Great Ryan_OC .
Yes it was successfull, you were able to access the database. You can change the QueryStr to access a table.- Ryan_OC2 years agoHelper I
Thanks so much! I don't understand why the first method was unable to work...is it a fabric issue do you think?
- Anonymous2 years agoNot applicable
Hi Ryan_OC ,
I am not sure of that.
Have you followed below format?tenant_id = "tenant_id" service_principal_id = f"client_id@{tenant_id}" # important to include your @fully qualified domain or tenant_id service_principal_secret = "client_secret"