在我们的工作场所,我们有一个 ASP.NET 应用程序,它使用 Microsoft SQL Server 2016 网络版数据库服务器作为后端数据库。
首先,在 ASP.NET 应用程序 web.config 中,我将数据库连接字符串配置为:
Password=blahPasswordblah;
Persist Security Info=True;
User ID=sa;
Initial Catalog=yadaDatabaseNameyada;
Data Source=blahDNSServerNameblah\yadaSQLServerInstanceNameyada
它似乎连接正常。但是,当 ASP.NET 应用程序中的代码尝试查询特定 View 时,我注意到一些奇怪的事情。查询将返回一个空结果集。
但是,当我更改连接字符串以包含工作站 ID 和应用程序名称时:
Password=blahPasswordblah;
Persist Security Info=True;
User ID=sa;
Initial Catalog=yadaDatabaseNameyada;
Data Source=blahDNSServerNameblah\yadaSQLServerInstanceNameyada;
workstation id=blahWorkStationIDblah;
application name=blahApplicationNameBlah
对相关 View 的查询将返回我预期的正确数据。
这里是关于 View 的元数据:
USE yadaDatabaseNameyada;
SELECT *
FROM INFORMATION_SCHEMA.VIEWS
WHERE table_name = 'blahViewOfInterestblah'
TABLE_CATALOG
yadaDatabaseNameyada
TABLE_SCHEMA
dbo
TABLE_NAME
blahViewOfInterestblah
VIEW_DEFINITION
Create View dbo.blahViewOfInterestblah ..........
CHECK_OPTION
NONE
IS_UPDATABLE
NO
当我在数据库连接字符串中包含工作站 ID 和应用程序名称时,为什么对相关 View 的查询会返回实际的正确结果?
我之所以这样问,是因为当我们最终将 ASP.NET 应用程序部署到生产环境时,在数据库连接字符串中包含“工作站 ID”属性和“应用程序名称”属性是一种糟糕的做法。
为什么它不能使用“工作站 ID”属性和“应用程序名称”属性?
最佳答案
虽然这是一个老问题,但我一直在寻找关于这个主题的信息,所以作为对这个问题的部分回答,并且在不了解更多关于数据库、服务器和应用程序用户帐户的配置的情况下,这是我的简短回答:
根据 Microsoft ,Application(APP)和Workstation ID(WSID)都是可选的:
APP Name of the application calling SQLDriverConnect (optional). If specified, this value is stored in the master.dbo.sysprocesses column program_name and is returned by sp_who and the APP_NAME functions.
WSID The workstation ID. Typically, this is the network name of the computer on which the application resides (optional). If specified, this value is stored in the master.dbo.sysprocesses column hostname and is returned by sp_who and the APP_NAME and the HOST_NAME function.
您是否尝试过更改应用程序 SQL 用户的权限或将其添加到 DBO 角色?
关于c# - 为什么我必须在数据库连接字符串中包含 “workstation id” 和 "application name"属性才能正确查询 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45572848/