使用Python通过Windows身份验证连接到SQL Server视图?

问题描述 投票:0回答:1

我可以访问 SQL Server 数据库中的以下视图:

[Serverlocation\Server].[Server.DW.Application].[Application_AdHoc].[DepartmentDailySummary]

我是初学者,对数据库和视图还不太了解。我收到“InterfaceError 28000”,它拒绝访问我的用户。

我试着把它放在这里:

conn_str = (
r'Driver=SQL Server;'
r'Server=Serverlocation\Server;'
r'Database=[Server.DW.Application].[Application_AdHoc].[DepartmentDailySummary];'
r'Trusted_Connection=yes;'
)

cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor(
sql sql-server-2008 odbc pyodbc windows-authentication
1个回答
0
投票

首先将连接字符串更改为以下代码:

conn_str = (
    r'Driver=SQL Server;'
    r'Server=Serverlocation\Server;'
    r'Database=Server.DW.Application.Application_AdHoc.DepartmentDailySummary;'
    r'Trusted_Connection=yes;'
)

并使用以下更改:

import pyodbc

# Construct the connection string
conn_str = (
    r'Driver=SQL Server;'
    r'Server=Serverlocation\Server;'
    r'Database=Server.DW.Application.Application_AdHoc.DepartmentDailySummary;'
    r'Trusted_Connection=yes;'
)

# Establish a connection and create a cursor
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()

# Now, you can use the cursor to interact with the database

检查连接字符串中必要的权限以及正确的服务器和数据库名称,以成功连接到 SQL Server 数据库。如果您仍然遇到问题,请提供有关您的设置的更多信息以及任何具体错误消息以获得进一步帮助。

© www.soinside.com 2019 - 2024. All rights reserved.