从Python连接到远程桌面上的MS SQL Server

问题描述 投票:0回答:1

我正在通过Windows服务器通过VBA使用以下代码连接到远程桌面上托管的SQL Server:

Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset

'Open Connection
objMyConn.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa;Password=xxxxx;Persist Security Info=True;Initial Catalog=databaseName;Data Source=192.168.1.xxx;"
objMyConn.Open

当前正在尝试使用python通过以下代码连接到相同的SQL Server数据库:

import pyodbc
server_name='192.168.1.xxx'
db_name='databaseName'
username='sa'
password='xxxxx'
conn = pyodbc.connect('DRIVER={ODBC Driver 11 for SQL Server};'
                      'Server=server_name;'
                      'Database=db_name;' 
                      'UID=username;'
                      'PWD=password;'
                      'Trusted_Connection=yes;')
cursor=conn.cursor()

回溯:

File "x/test.py", line 6, in <module>
    conn = pyodbc.connect('DRIVER={ODBC Driver 11 for SQL Server};'
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].  (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. 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)')
python sql sql-server database-connection remote-server
1个回答
0
投票
由于要提供UID和PWD,因此应省略参数trusted_connectionTrusted_connection用您当前的Windows用户值填充UID和PWD值,因此它可能保留用于主机内的本地连接。我相信连接字符串应如下所示:

'DRIVER={ODBC Driver 11 for SQL Server};' 'Server=server_name;' 'Database=db_name;' 'UID=username;' 'PWD=password;'

© www.soinside.com 2019 - 2024. All rights reserved.