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

使用C#控制台应用(System.Data)无法连接Azure SQL DB求助

Troubleshooting "Works on My Machine" Azure SQL Connection Issue

我现在快愁秃了,碰到了开发者圈里经典的“在我机器上正常运行”的困境!

我写了一个C#控制台应用,用来往Azure托管的SQL数据库上传数据。其中开发环境的数据库是Visual Studio订阅(MSDN免费版)的实例,生产环境的数据库则在公司的企业Azure订阅里,相关配置得依赖我们的Azure管理员团队来弄。

在我本地PC上,不管是在Visual Studio里运行,还是直接用命令行执行,都能正常连接这两个数据库实例。但是到了生产服务器上,我用完全相同的测试应用,却只能成功连接开发数据库,生产数据库死活连不上。

补充几个关键信息:

  • 我们的网络环境是在公司防火墙/代理之后
  • 两个Azure SQL数据库的防火墙规则都已经添加了生产服务器对应的IP地址(毕竟本地能连说明基础配置没问题?)

为了排查问题,我特意写了个极简的测试程序,用对应的生产库连接字符串运行:

using System; 
using System.Data.SqlClient; 

namespace AzureConnectTest { 
    class AzureConnectTest { 
        static void Main(string[] args) { 
            string ConnectionString = @"data source=******.database.windows.net; "; 
            ConnectionString += @"initial catalog=****; "; 
            ConnectionString += @"persist security info=False; "; 
            ConnectionString += @"MultipleActiveResultSets=True; "; 
            ConnectionString += @"user id=""*****""; "; 
            ConnectionString += @"password=""*****""; "; 

            Console.WriteLine("Attempting to connect"); 
            Console.WriteLine(ConnectionString); 

            try { 
                using (SqlConnection conn = new SqlConnection(ConnectionString)) { 
                    conn.Open(); 
                    Console.WriteLine(" - Success"); 
                } 
            } catch (Exception ex) { 
                Console.WriteLine(" - Failed"); 
                Console.WriteLine(ex.Message); 
            } 
        } 
    } 
}

运行这个测试程序后,连接生产数据库的请求直接失败了。我已经在Stack Overflow上搜过一堆类似的问题,但要么没找到可行的答案,要么那些解决方案对我的情况完全没用,实在没办法了来求助各位!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:26:27