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

Azure上ASP.NET Core 6应用连接GoDaddy数据库时连接字符串格式错误排查

ASP.NET Core 6 连接GoDaddy数据库时的连接字符串格式错误排查

我在Azure上部署了ASP.NET Core 6 Web应用,需要连接GoDaddy提供的数据库,但遇到连接字符串格式错误,无法定位原因。当前在Azure环境变量和项目的appsettings.json中配置的连接字符串如下:

"Server=tcp:NAMESERVERGODADDY:1433;Initial Catalog=NAMEDB;Persist Security Info=False;User ID=USERID;Password=PASS;MultipleActiveResultSets=True;TrustServerCertificate=False;"

日志报错信息:

An unhandled exception has occurred while executing the request.
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 262.
at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
at System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary2 parsetable, String connectionString, Boolean buildChain, Dictionary2 synonyms, Boolean firstKey)
at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms, Boolean useOdbcRules)
at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
at Microsoft.Data.SqlClient.SqlConnectionStringBuilder..ctor(String connectionString)

排查与解决步骤

1. 重点检查密码字段的特殊字符

报错索引262位置大概率对应密码段,GoDaddy生成的密码常包含分号、引号、反斜杠等特殊字符,这些会直接破坏连接字符串的格式规则:

  • 若密码含特殊字符,用双引号包裹密码,例如:Password="P@ssw;rd123";
  • 避免手动转义特殊字符(如\\),双引号包裹是最可靠的处理方式。

2. 清理多余的引号与转义符

  • 在appsettings.json中,连接字符串本身已被外层双引号包裹,内部密码的双引号需转义,正确格式示例:
    "ConnectionStrings": {
      "DefaultConnection": "Server=tcp:NAMESERVERGODADDY:1433;Initial Catalog=NAMEDB;Persist Security Info=False;User ID=USERID;Password=\"P@ssw;rd\";MultipleActiveResultSets=True;TrustServerCertificate=False;"
    }
    
  • Azure环境变量中直接填写字符串,不要加外层双引号,例如直接输入:Server=tcp:NAMESERVERGODADDY:1433;Initial Catalog=NAMEDB;Persist Security Info=False;User ID=USERID;Password="P@ssw;rd";MultipleActiveResultSets=True;TrustServerCertificate=False;

3. 匹配GoDaddy数据库的连接规则

GoDaddy的SQL Server有特定配置要求:

  • 确认服务器地址格式正确,通常为xxx.db.xxxxxx.hostedresource.com,不要遗漏tcp:前缀
  • 核对端口是否为1433,部分实例可能使用自定义端口,可在GoDaddy控制面板查看
  • 将TrustServerCertificate改为True,GoDaddy的证书可能无法被Azure环境直接信任,这是常见适配项

4. 验证连接字符串有效性

用SQL Server Management Studio等本地工具测试连接字符串,确认能正常连接GoDaddy数据库,排除数据库本身的访问权限或地址错误。

5. 确认Azure配置优先级

ASP.NET Core中环境变量的配置优先级高于appsettings.json,确保Azure环境变量中的连接字符串与本地测试通过的版本完全一致,无语法错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:04:51