1

我将 ASP.NET Core 应用程序发布到 Windows Server 2012 环境。我创建了一个新网站,将其指向该文件夹,并且运行良好。现在我正在尝试使应用程序连接到数据库。我安装了 SQL Server Express 2016 并编辑了我的项目,以便从数据库中检索数据。将我的“KSP_Backend.mdf”文件放到我的服务器上,并将该文件附加到 SQL Server Management Studio 中。数据库是完全可见的,所以它存在。

现在,当我尝试在已发布的项目文件夹“dotnet KSP_Backend.dll”中运行我的项目时,它给了我以下错误:

     Unhandled Exception: System.Data.SqlClient.SqlException: 
        A network-related or instance-specific error occurred while establishing 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: 50 - Local Database Runtime error occurred. Thespecified LocalDB instance does not exist.)

这是我使用的“appsettings.json”文件中的连接字符串:

"Server=<My server's name>\\KSPSERVER;Database=KSP_Backend;Trusted_Connection=True;MultipleActiveResultSets=true"

我只是不确定连接字符串是否导致了我的错误。我的防火墙已关闭,LocalDB 已启动并运行。

4

1 回答 1

0

当我没有在数据库服务器上启用“远程连接”时,我遇到了同样的问题。在 SQL Management Studio 中,右键单击服务器名称,然后Properties转到选项卡Connections,确保Allow remote connections已启用。

另一件事可能是未启用 SQL Server TCP/IP 协议。在SQL Server Configuration Manager中,单击SQL Server Network Configuration左窗格中的 并右键单击TCP/IP并选择Enable

最后,您可能需要配置 SQL Server Express 以侦听静态端口。您可以在此处找到关于这一点和前面几点的很好的参考:SQL Express Remote Connections

于 2017-08-04T14:06:47.040 回答