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

无法将C#应用连接至Azure SQL数据库,求助排查方案

C#连接Azure SQL数据库超时问题求助

我知道这个问题已经被多次提及,但浏览所有相关帖子后仍未解决。我正尝试将C#应用连接至Azure SQL数据库以插入数据。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "claimsportaldbserver.database.windows.net";
builder.UserID = "username@domain.com@claimsportaldbserver";
builder.Password = "password";
builder.InitialCatalog = "Claims Portal_db";

try
{
    using (Microsoft.Data.SqlClient.SqlConnection connection = new Microsoft.Data.SqlClient.SqlConnection(builder.ConnectionString))
    {
        String sql = "SELECT * FROM [dbo].[Claims]";

        using (Microsoft.Data.SqlClient.SqlCommand command = new Microsoft.Data.SqlClient.SqlCommand(sql, connection))
        {
            connection.Open();
            using (Microsoft.Data.SqlClient.SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
                }
            }
        }
    }
}
catch (Exception ex)
{
    return ex.InnerException.ToString();
}

以下是我已尝试的解决步骤(来自各类论坛及文档):

  • 从Azure复制连接字符串信息,确保无拼写错误
  • 将我的IP地址添加至Azure防火墙规则
  • 在本地防火墙规则中添加端口1433
  • 使用Microsoft.Data.SqlClient而非System.Data.SqlClient
  • 在代码中添加ServicePointManager.SecurityProtocol配置
  • 在代码中添加WebRequest.DefaultWebProxy.Credentials配置

但仍收到错误:

建立与SQL Server的连接时出现网络或实例错误,服务器未找到或不可访问。(TCP提供程序:等待操作超时)

附出站防火墙规则截图:
出站防火墙规则

恳请各位提供帮助,谢谢。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 05:52:44