1

Attempting to connect to an Azure SQL database, most other configurations I've tried result in an instant error:

Client unable to establish connection (0) (SQLDriverConnect)')

But this one I'm currently trying times out and is the closest I've got:

cnxn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}', server='tcp:mydatabase.database.windows.net:1433', database='MyDatabase', user='username', password='password')

pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

dswire
  • 21
  • 1
  • Did you try configure and test connection with windows odbc data sources ? Maybe you didn’t exposed correctly your db on network – Devyl Jul 13 '22 at 06:25
  • the same configuration works perfectly in datagrip and sqlpro – dswire Jul 13 '22 at 06:28
  • On same computer ? With same network configurations ? Like vpn etc – Devyl Jul 13 '22 at 06:30
  • try with raw connection string like pyodbc.connect('SERVER=host;PORT=port;DATABASE=default database name;DRIVER={SQL Server Native Client 11.0};UID=user;PWD=password'); host without the 'tcp:' – Devyl Jul 13 '22 at 06:33

2 Answers2

0

There is no requirement for the connection string to include the default SQL Server port, which is 1433.

Add server name without tcp, use tcp: before quotes of servername, Server name format is servername.database.windows.net

Example String

pyodbc.connect('DRIVER='+driver+';SERVER=tcp:'+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

enter image description here

Pratik Lad
  • 4,343
  • 2
  • 3
  • 11
0

The port defined on the server attribute is separated by comma and not colon.

Driver={ODBC Driver 1x for SQL Server};Server=[tcp]:{serverName}.database.windows.net[,1433]; (...)

You can check a sample of the connection string on the connections string blade on your database page on Azure Portal.