pymssql._pymssql.OperationalError处理方法python无法连接sqlserver2008r2 的方法

pymssql._pymssql.OperationalError: (20009, b’DB-Lib error message 20009, severity 9:\nUnable to conn
severity 9:\nAdaptive Server connection failed
打开sql server 配置管理器。
在这里插入图片描述
将TPC/IP 启用
在这里插入图片描述
import pyodbc

Replace ‘server_name’, ‘database_name’, ‘username’, and ‘password’ with your server name, database name, username, and password

conn = pyodbc.connect(‘DRIVER={ODBC Driver 17 for SQL Server};SERVER=server_name;DATABASE=database_name;UID=username;PWD=password’)

cursor = conn.cursor()# Replace ‘table_name’ with the name of your table

cursor.execute(‘SELECT * FROM table_name’)

for row in cursor:

print(row)

Close the connectionconn.close()