远程连接到 MS SQL - 使用 pyodbc 时出错与使用 SQL Server Management Studio 成功 [英] Remote connection to MS SQL - Error using pyodbc vs success using SQL Server Management Studio

查看:35
本文介绍了远程连接到 MS SQL - 使用 pyodbc 时出错与使用 SQL Server Management Studio 成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一网络中但在另一台计算机上有一个 MS SQL 数据库.使用 SQL Server Management Studio (SSMS) Express,我可以毫无问题地找到数据库并进行连接.

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.

但是当我使用 pyodbc 连接到同一台服务器时:

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

import pyodbc

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

我收到以下错误:

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

OBS:我想服务器字符串应该是正确的,因为如果我更改它,我总是会收到以下错误:

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)')

此处显示使用 SQL Server Studio Express 进行远程连接时成功的图像.

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

推荐答案

尝试指定端口:

import pyodbc

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

如果仍有问题,请尝试使用服务器的 IP 或 FQDN.

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

这篇关于远程连接到 MS SQL - 使用 pyodbc 时出错与使用 SQL Server Management Studio 成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆