I know this question has been asked many times on the web resulting in many different solutions none which have worked for me.
For my scenario I'm attempting to do a simple connection to a MS Sql database connecting just with Service account username and password using windows authentication isn't an option for my task.
This is the connection string that I am providing:
databaseConnection = 'DRIVER={SQL Server}; SERVER=ServerName; Database=DatbaseName; UID=UserId; PWD=password;'
This is the error I receive when trying to run the script:
dbConnection = pyodbc.connect(DATABASE.databaseConnection)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver]
[SQL Server]Cannot open database "XXXX" requested by the login. The login failed.
(4060) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver]
[SQL Server]Cannot open database "XXXX" requested by the login. The login failed. (4060)')
Is there anything incorrect about this connection string? I have double checked the username and password by copy and pasting it into SQL Server Management Studio and logging into the database from there and it is successful.
5条答案
按热度按时间33qvvth11#
I had this problem myself and I fixed it by going to the Microsoft SQL Server management studio and manually connecting the database to the user account you login into the database with.
To do this:
While you are at it, look at the panel at the bottom. Where it says
Database role membership for:{database name}
, make sure you havedb_datareader
anddb_datawriter
checked.8wigbo562#
Here is a way to have a connection string using sqlalchemy (see https://www.sqlalchemy.org/ ):
3bygqnnd3#
I faced the same issue and the problem was the database name.
I was putting the database name between brackets, like [DB_NAME], however as soon as I removed the brackets to DB_NAME it worked fine.
Please give it a try.
pieyvz9o4#
在obdc数据源中配置MSSQL服务器,并为pyodbc连接给予名称,如下所示。
这对我很有效。如果有其他方法可以做到这一点,请告诉我。
q43xntqr5#
The important parameter is where the “username and password” were set, I am replacing it with “Trusted_Connection=yes” so the user account logged into Windows is used instead.