4

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.

7
  • 2
    Did you try User Id=myUsername; Password=myPassword; ? Commented Mar 1, 2014 at 3:36
  • 1
    @YevgenYampolskiy Changing the attributes to User Id and Password doesn't work. Receive an error for invalid attributes Commented Mar 3, 2014 at 21:31
  • Are you attempting to connect using a Windows Service? Commented Apr 2, 2014 at 20:43
  • @þăדᴚῖↄқ I have resolved this thanks Commented Apr 2, 2014 at 22:20
  • 1
    Want to share your resolution? I came across this question while trying to solve a similar problem. Commented Apr 3, 2014 at 13:31

6 Answers 6

4

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:

  • Open Microsoft SQL Server management studio and connect to your server.
  • Go to login, right click on the correct user, and go to properties.
  • On the top left there's a panel (titled "select a page"). Go to User mapping.
  • On the table displayed, where it says user (second column), type in the name of the user next to the database you are using.

While you are at it, look at the panel at the bottom. Where it says Database role membership for:{database name}, make sure you have db_datareader and db_datawriter checked.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by 'go to login'? I'm in SSMS, have connected to my server, but I can't see where I would login?
Expand the database in Object Explorer, then go Security -> Logins.
1

Here is a way to have a connection string using sqlalchemy (see https://www.sqlalchemy.org/):

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from db import Db

#Get localhost DB
engine = create_engine('mssql+pyodbc://myDbUser:myDbPassword@localhost:1433/MyDatabase?driver=SQL+Server+Native+Client+11.0?trusted_connection=no')

DBSession = sessionmaker(bind=engine)

session = DBSession()
db = Db(session)
print "the widget number is: "+db.widgetNumber

Comments

1

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.

Comments

0

Configure the MSSQL server in the obdc datasources and give the name with the pyodbc connection as below.My userdsn name is MyConnection

import pyodbc

cnxn = pyodbc.connect('DSN=MyConnection')
cursor = cnxn.cursor()
cursor.execute("SELECT TOP (1000) [DepartmentID]      ,[Name]      ,[GroupName]      ,[ModifiedDate]  FROM ["
               "AdventureWorks2019].[HumanResources].[Department]")
rows = cursor.fetchall()
for row in rows:
    print(row.DepartmentID, row.Name)

It is working for me. Let me know if there are any other ways to do this.

Comments

0

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.

import pyodbc

conn_str = ("Driver={SQL Server};"
                           "Server=<server-name>;"
                            "Database=<db-name>;"
                            "Trusted_Connection=yes")

conn = pyodbc.connect(conn_str)

cursor = conn.cursor()

Comments

0

I also faced a similar issue and the problem was the database name.

I was adding hierarchy in the database name , like "DB_NAME.dbo", however as soon as I removed it to to DB_NAME it worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.