python使用pyodbc通过不同认证方式连接sqlserver数据源
- 1. 普通sqlserver认证连接
- 2. Azmure Microsoft Entra Password认证连接
- 3. 源码
- 参考
1. 普通sqlserver认证连接
2. Azmure Microsoft Entra Password认证连接
3. 源码
import pyodbcprint(pyodbc.drivers())
connection_string = ("DRIVER={ODBC Driver 17 for SQL Server};""SERVER=localhost;""DATABASE=testdb;""UID=your_username;""PWD=your_password;"
)
connection_string = ("DRIVER={ODBC Driver 17 for SQL Server};""SERVER=localhost;""DATABASE=testdb;""UID=your_username;""PWD=your_password;""Authentication=ActiveDirectoryInteractive;" "Connection Timeout=30;"
)
conn = pyodbc.connect(connection_string)
cursor = conn.cursor()
cursor.execute("SELECT user_id, user_name FROM users")
rows = cursor.fetchall()
for row in rows:print(f"User ID: {row.user_id}, User Name: {row.user_name}")
参考
- https://www.cnblogs.com/ljhdo/p/13608916.html