Based on this old thread :
If your server name is blahserver.sql.azuresynapse.net and your login is myser@email.com
si then you may need to supply your login as myser@email.com@blahserver.sql.azuresynapse.net
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm getting an error when using my Python program to connect to an Azure Synapse MS SQL server.
My info:
I installed the MS ODBC driver on my laptop, I did not configure it. And installed pyodbc for my program in Test\piecework. When my Python program tries to connect to the database on Azure I get this error:
pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Cannot open server "
[xxx.com
]" requested by the login. The login failed. (40532)
(SQLDriverConnect); [HY000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Cannot open server "
[xxx.com
]" requested by the login. The login failed. (40532)')
My second try.
When I use this for the connection string DRIVER={ODBC Driver 18 for SQL Server};SERVER=myemail@domain.com@blahsomething.sql.azuresynapse.net;DATABASE=pace;UID=myemail@domain.com;PWD=mypass;PORT=1433;Authentication=ActiveDirectoryPassword
I get this error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to myuser@blahsomething.sql.azuresynapse.net. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)')
Questions
Based on this old thread :
If your server name is blahserver.sql.azuresynapse.net and your login is myser@email.com
si then you may need to supply your login as myser@email.com@blahserver.sql.azuresynapse.net
Ok this is how I got it to work. I hope this helps others.
# Make connection details.
username = 'user@email.com' # For Microsoft Active Directory username
password = 'password' # Active Directory password
dbport = 1433
server = f'myserver.sql.azuresynapse.net'
database = 'dbname'
driver = '{ODBC Driver 18 for SQL Server}'
# We might need this in conn string: Authentication=ActiveDirectoryPassword;
connection_string = f'DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password};PORT={dbport};Authentication=ActiveDirectoryPassword'
# print(f"Connecting with connection string: {connection_string}...")
try:
# Establish a connection
conn = pyodbc.connect(connection_string)
if not conn:
errstr = f"ERROR: connection not made to {connection_string}"
print(f"{errstr}")
sys.exit()
# Create a cursor
cursor = conn.cursor()