简体   繁体   English

如何使用 pyodbc 在另一台电脑上连接到 Microsft SQL Sever 2005?

[英]How to connect to Microsft SQL Sever 2005 in another pc using pyodbc?

I am trying to connect to a database, from a Windows 10, that is on another pc in the same local network using the pyodbc module.我正在尝试使用 pyodbc 模块从 Windows 10 连接到数据库,该数据库位于同一本地网络中的另一台 PC 上。 The external pc is a Windows Server 2003 and it has a database Microsoft SQL Server 2005. I am not sure how the connection string has to be, I have tried with the following code:外部电脑是 Windows Server 2003,它有一个数据库 Microsoft SQL Server 2005。我不确定连接字符串必须如何,我尝试使用以下代码:

import pyodbc

DRIVER = "{ODBC Driver 11 for SQL Server}" # supports SQL Server 2005 through 2014
SERVER = "192.168.100.101" # the ip of the windows server 2003
PORT = "1433" # I am not sure if this parameter is needed
DATABASE = "database_name" # name of the database
USER = "user"
PASS = "psw"

string_connection = f"DRIVER={DRIVER};SERVER={SERVER};PORT={PORT};DATABASE={DATABASE};UID={USER};PWD={PASS}"

cnxn = pyodbc.connect(string_connection)
cursor = cnxn.cursor()

cursor.close()
cnxn.close()   

But when I run the code above I get the following error:但是当我运行上面的代码时,出现以下错误:

InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

Data source name not found and no default driver specified未找到数据源名称且未指定默认驱动程序

This error message indicates the driver specified in the connection string is not installed on the client machine.此错误消息表明连接字符串中指定的驱动程序未安装在客户端计算机上。 Run odbcad32.exe on the Win10 machine and click the drivers tab to view the list of installed ODBC drivers.在Win10机器上运行odbcad32.exe,点击驱动选项卡,查看已安装的ODBC驱动列表。

To remediate, you can either change the connection string to use an already installed SQL Server ODBC driver, or install the ODBC Driver 11 for SQL Server from here . To remediate, you can either change the connection string to use an already installed SQL Server ODBC driver, or install the ODBC Driver 11 for SQL Server from here .

Be aware that Windows Server 2003 and SQL Server 2005 have been out of support for several years.请注意,Windows Server 2003 和 SQL Server 2005 已经停止支持好几年了。 The oldest support Windows server version is 2012 and SQL 2012 is the oldest SQL version as of this writing.最旧的支持 Windows 服务器版本是 2012 年,SQL 2012 是截至本文撰写时最旧的 SQL 版本。 I suggest you migrate to newer versions.我建议您迁移到较新的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM