简体   繁体   中英

pyodbc - Unable to login using trusted connection

I am able to login to SQL Server in SSMS using Windows authentication.

{Image removed due to exposure of real email address}

But when I try the same using pyodbc it fails:

import pyodbc

cnxn_str = (
    r'DRIVER=ODBC Driver 17 for SQL Server;'
    r'SERVER=.\SQLEXPRESS02;'
    r'Trusted_Connection=yes;'
    r'DATABASE=Scenario2025Max_Release 5.0_0523586A6813B825D031545D6E25A815;'
)

cnxn = pyodbc.connect(cnxn_str)

I get this error:

Traceback (most recent call last):

File "", line 30, in
pyodbc.InterfaceError: ('28000', '[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'MicrosoftAccount\\XXXXXXXX@outlook.com'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "Scenario2025Max_Release 5.0_0523586A6813B825D031545D6E25A815" requested by the login. The login failed. (4060); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'MicrosoftAccount\\XXXXXXXXAccount@domain.com'. (18456); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "Scenario2025Max_Release 5.0_0523586A6813B825D031545D6E25A815" requested by the login. The login failed. (4060)')

How is this possible?

UPDATE - Added SQL Server logs for the error run - 在此处输入图片说明

I went through at least a hundred web pages over 2 full days on this, tried everything that was posted on StackOverflow threads, SQL/pyodbc/connectionsstrings blogs - restarting SQL Server, restarting my computer, modifying connection strings, changing SQL Server user permissions, but nothing worked.

Ultimately, I re-installed SQL Server entirely and I don't get the login failed error anymore, at least for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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