python pyodbc:如何连接到特定的实例

时间:2021-11-21 11:50:17

Am trying to connect to a specific instance of SQL Server and get some data from system tables. Am connecting using this code snippet:

我试图连接到特定的SQL Server实例并从系统表中获取一些数据。我使用此代码段进行连接:

connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102;DATABASE=master;INSTANCE=instance1;UID=sql2008;PWD=password123;Trusted_Connection=yes')
...
cursorObj.execute("select * from sys.dm_os_sys_info")
row = cursorObj.fetchone()
print("rows from table ",row) 

however am getting the values for the default instance only, but not able to get the value for 'instance1'. So, giving instance name in 'INSTANCE=instance1' really seems to have no effect. Even without it (tried giving 'PORT=1443', the instance's port number), am getting the values only for the default SQL Server instance. How to force it to get the values for the specific instance?

但是我只获取默认实例的值,但无法获取'instance1'的值。因此,在'INSTANCE = instance1'中给出实例名称似乎没有任何效果。即使没有它(尝试给出'PORT = 1443',实例的端口号),我只获取默认SQL Server实例的值。如何强制它获取特定实例的值?

1 个解决方案

#1


13  

Authentication

First, you're providing both uid/pwd (SQL Server authentication) and trusted_connection (Windows authentication). Pick one, you can't use both. I'll assume SQL Server authentication for the following examples.

首先,您提供了uid / pwd(SQL Server身份验证)和trusted_connection(Windows身份验证)。选择一个,你不能同时使用。我将假设以下示例的SQL Server身份验证。

Connection strings

Connecting to named instance instance1 using the instance name:

使用实例名称连接到命名实例instance1:

connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102\instance1;DATABASE=master;UID=sql2008;PWD=password123')

Connecting to named instance using TCP/IP using the port number 1443:

使用端口号1443使用TCP / IP连接到命名实例:

connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102,1443;DATABASE=master;UID=sql2008;PWD=password123')

Keyword alternative

pyodbc.connect() supports keywords, I think these are easier to read and you don't have to do any string formatting if you're using variables for connection string attributes:

pyodbc.connect()支持关键字,我认为这些更易于阅读,如果您使用连接字符串属性的变量,则不必进行任何字符串格式化:

Named instance:

connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
                               server='192.106.0.102\instance1',
                               database='master',
                               uid='sql2008',pwd='password123')

TCP/IP port:

connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
                               server='192.106.0.102,1443',
                               database='master',
                               uid='sql2008',pwd='password123')

#1


13  

Authentication

First, you're providing both uid/pwd (SQL Server authentication) and trusted_connection (Windows authentication). Pick one, you can't use both. I'll assume SQL Server authentication for the following examples.

首先,您提供了uid / pwd(SQL Server身份验证)和trusted_connection(Windows身份验证)。选择一个,你不能同时使用。我将假设以下示例的SQL Server身份验证。

Connection strings

Connecting to named instance instance1 using the instance name:

使用实例名称连接到命名实例instance1:

connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102\instance1;DATABASE=master;UID=sql2008;PWD=password123')

Connecting to named instance using TCP/IP using the port number 1443:

使用端口号1443使用TCP / IP连接到命名实例:

connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102,1443;DATABASE=master;UID=sql2008;PWD=password123')

Keyword alternative

pyodbc.connect() supports keywords, I think these are easier to read and you don't have to do any string formatting if you're using variables for connection string attributes:

pyodbc.connect()支持关键字,我认为这些更易于阅读,如果您使用连接字符串属性的变量,则不必进行任何字符串格式化:

Named instance:

connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
                               server='192.106.0.102\instance1',
                               database='master',
                               uid='sql2008',pwd='password123')

TCP/IP port:

connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
                               server='192.106.0.102,1443',
                               database='master',
                               uid='sql2008',pwd='password123')