2

I have tried both pypyodbc and pymssql with no luck using Windows Authentication. Any ideas? I can log in when I use both the username and password but I do not want to do this. I want to use Windows Auth.

I actually have only been able to get pypyodbc working. I get different connection errors with pymssql. I have Googled different examples and they are not working. I figure I am not using the correct format for username or something. Do I have to put "DomainName\myusername" in there instead of just my username? I have tried this with no luck.

How could I convert this to Windows Auth login?

import pypyodbc
connection = pypyodbc.connect('Driver={SQL Server};'
                                'Server=IPaddresshere;'
                                'Database=PythonTest;'
                                'uid=myuser;pwd=mypass')

cursor = connection.cursor() 
SQLCommand = ("SELECT FirstName, LastName, UID "      
    "FROM dbo.member "
    "WHERE UID = ?")
Values = [2]
cursor.execute(SQLCommand,Values)
results = cursor.fetchone()
# note: UID column is an integer.  You can't normally take an integer and place it in a string.
# so you must add str(results[2])
print("Employee " + results[0] + " " + results[1] + " ID is " + str(results[2]))
connection.close()

2 Answers 2

5

In the case when you want to use Windows authentication, your connection string should look like:

Driver={SQL Server};'
'Server=IPaddresshere;'
'Database=PythonTest;'
'Trusted_Connection=True;'
Sign up to request clarification or add additional context in comments.

Comments

1

Try setting Trusted Connection:

pyodbc.connect(r'Driver={SQL Server};Server=DBINSTANCE;Database=myDB;Trusted_Connection=yes;')

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.