gpt4 book ai didi

c# - 从 Windows 服务通过网络访问 SQL Server

转载 作者:行者123 更新时间:2023-11-30 21:20:39 24 4
gpt4 key购买 nike

我已经创建了一个将连接到 SQL Server 以运行存储过程的类。在 Windows 窗体解决方案中使用此类时,可以成功访问数据库。将该类放入 Windows 服务时,出现以下错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我怀疑问题与权限有关。

这是相关代码部分:

SqlConnection conn = new SqlConnection(j.ConnectionString);
SqlCommand cmd = new SqlCommand(j.Query, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet("Table1");

da.Fill(ds, "Table1"); // <----- error occurs here (Windows Service only)

SQL Server 版本是

Microsoft SQL Server 2008 (SP1) - 10.0.2766.0 (X64)   
Enterprise Edition (64-bit) on Windows NT 5.2 <X64>
(Build 3790: Service Pack 2)

Visual Studio 版本是 Microsoft Visual C# 2008框架版本为 3.5 SP1

最佳答案

当使用默认的“LocalService”登录时,Windows 服务被限制使用网络资源。因此,您需要在允许访问网络的用户帐户下运行该服务。以下是可能性:

1.) 如果您使用“NetworkService”连接,则不应在连接到 SQL 服务时使用 Windows 的集成身份验证,但您可以使用带有用户名和密码的 SQL 身份验证。 (从技术上讲,如果您授予客户端计算机的域帐户(例如 CLIENT$)对数据库的权限,您也许可以使用集成身份验证……但不建议这样做。)

2.) 如果您将服务设置为在您选择的真实用户帐户下运行,则 SQL 连接可以使用 Windows 集成身份验证,但您必须授予该帐户对数据库的权限。

关于c# - 从 Windows 服务通过网络访问 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3158601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com