Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"But why ...?"

For those interested in why SQL Server Management Studio (SSMS) can connect to servernameinstance while other applications (like our pyodbc apps) cannot, it's because SSMS keeps an MRU (Most Recently Used) list of port numbers in the Windows registry at

HKEY_LOCAL_MACHINESOFTWAREMicrosoftMSSQLServerClientSuperSocketNetLibLastConnect

HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftMSSQLServerClientSuperSocketNetLibLastConnect

Each MRU entry (registry value) looks something like this:

Name: PANORAMASQLEXPRESS
Type: REG_SZ 
Data: -1006030326:tcp:PANORAMA,52865

Once SSMS has successfully connected by instance name via the SQL Browser service on the remote machine, it can continue to connect by instance name even if the SQL Browser is no longer running on the remote machine, provided that the port number has not changed. Apps that don't use this MRU list (like our pyodbc app) need to have the SQL Browser service running on the remote machine every time they want to connect by instance name.

The most common scenario:

  • I want to connect to YOUR-PCSQLEXPRESS. I try doing that from SSMS on MY-PC, but it doesn't work because the SQL Browser was installed with "Start Mode" set to "Manual" on YOUR-PC.
  • I ask you to start the SQL Browser service on YOUR-PC, and you kindly comply, but you just start the service and forget to change the "Start Mode" setting to "Automatic".
  • I am able to connect via SSMS (which caches the YOUR-PCSQLEXPRESS port in the MRU). My python app can connect, too.
  • After the next time YOUR-PC restarts, I can connect via SSMS (via the MRU) but my python app cannot (because the SQL Browser service is no longer running on YOUR-PC).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...