You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生产环境应用调用API触发SQL Server连接错误:参数不正确

SQL Server连接错误排查方案

触发的错误信息

Win32Exception: The parameter is incorrect.
Unknown location
SqlException: 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: 25 - Connection string is not valid)

当前配置信息

appsettings.json配置

{"Logging": {"LogLevel": {"Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information"}},"AllowedHosts": "*","ConnectionStrings": {"Hawk": "Data Source=Hawk.db","HawkSQLServer": "Server=vgcserver2\\MSSQLSERVER;Database=HawkNew;User Id=sa;Password=password;"}}

服务器web.config配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" 
                     modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\BackEnd.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>

排查步骤

  • 验证连接字符串实例名:SQL Server默认实例的名称为MSSQLSERVER,连接时直接使用服务器名即可,无需额外指定实例名。尝试修改连接字符串为:

    Server=vgcserver2;Database=HawkNew;User Id=sa;Password=password;
    

    JSON中保持转义格式即可。

  • 检查SQL Server远程连接配置:

    1. 打开SQL Server配置管理器,找到对应实例的「SQL Server网络配置」,确保TCP/IP协议处于启用状态。
    2. 双击TCP/IP协议,确认IP地址中的TCP端口(默认1433)已正确配置,且监听状态为启用。
  • 确认防火墙规则:检查vgcserver2服务器的防火墙,确保已开放SQL Server监听的端口(默认1433),允许应用服务器的IP地址访问。

  • 检查SQL Server服务状态:在vgcserver2服务器上,打开服务管理器,确认SQL Server服务(对应实例)处于运行状态。

  • 验证身份验证模式:打开SQL Server Management Studio,连接到目标实例,右键实例→属性→安全性,确认已启用「SQL Server和Windows身份验证模式」,否则sa账号无法登录。

  • 测试网络连通性:在应用服务器上执行以下命令:

    • ping vgcserver2:测试服务器是否可达
    • Test-NetConnection vgcserver2 -Port 1433(PowerShell命令):测试端口是否开放
  • 临时测试连接字符串参数:尝试添加TrustServerCertificate=True;Encrypt=False;到连接字符串中,排除SSL证书验证问题导致的连接失败(生产环境需谨慎使用,建议后续配置正确的证书)。

内容的提问来源于stack exchange,提问作者Noisemastering

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:54:26