无法将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
相关产品推荐
相关产品推荐

