如何在 Python 上使用 Windows Auth 登录 MSSQL?

Pro*_*rox 2 python sql-server pymssql python-3.x

我尝试过 pypyodbc 和 pymssql,但使用 Windows 身份验证没有成功。有任何想法吗?当我同时使用用户名和密码时可以登录,但我不想这样做。我想使用 Windows 身份验证。

实际上我只能让 pypyodbc 工作。我在使用 pymssql 时遇到不同的连接错误。我用谷歌搜索了不同的例子,但它们不起作用。我想我没有使用正确的用户名格式或其他格式。我是否必须在其中输入“DomainName\myusername”而不仅仅是我的用户名?我尝试过这个但没有运气。

如何将其转换为 Windows 身份验证登录?

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()
Run Code Online (Sandbox Code Playgroud)

And*_*yev 5

如果您想使用 Windows 身份验证,您的连接字符串应如下所示:

Driver={SQL Server};'
'Server=IPaddresshere;'
'Database=PythonTest;'
'Trusted_Connection=True;'
Run Code Online (Sandbox Code Playgroud)