Skip to content
Advertisement

Remote connection to MS SQL – Error using pyodbc vs success using SQL Server Management Studio

I have a MS SQL database in the same network but in other computer. Using the SQL Server Management Studio (SSMS) Express, I can find the database and connect without problems.

But when I use pyodbc to connect to the same server using:

import pyodbc

server = r"xxxERxxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)

I get following error:

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

OBS: I guess that the server string should be right, since if I change it I get always the following error:

pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)')

Here the image showing success while using SQL Server Studio Express to connect remotely.

enter image description here

Advertisement

Answer

Try specifying the port:

import pyodbc

server = r"xxxERxxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
port = "1433"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';PORT=' + port + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)

If you’re still having issues, try using the IP or FQDN of the server.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement