【发布时间】:2021-12-24 08:54:15
【问题描述】:
我为 Azure 函数设置了 Azure 混合连接,以便连接到本地 SQL Server 数据库。但是,我将以下内容添加为连接字符串;我在尝试连接时收到以下错误。此处有关如何格式化连接字符串以使用混合连接的指导将不胜感激。我在下面使用的错误消息、代码和连接字符串。
Azure 函数使用的是 .NET Framework,而我使用的是 SQL Server 2019
A connection was successfully established with the server, but then an error occurred during the login process (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
Azure 函数代码
using System.Linq
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using System.Data.SqlClient;
namespace AzureHybridConnectionTest
{
public static class TestOnPremConnection
{
[FunctionName("TestHybridConnection")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
{
log.Info("Testing Connection to On Premise SQL Database.");
string connectionString = System.Environment.GetEnvironmentVariable("HybridConnectionString");
log.Info(connectionString);
using(SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
log.Info($"SQL Connection open to database {conn.Database}");
}
log.Info("SQL Connection closed");
return req.CreateResponse(HttpStatusCode.OK, "Success!");
}
}
}
连接字符串
Server=tcp:[hybrid connection endpoint];Initial Catalog=[db name];Persist Security Info=False;User ID=[user];Password=[password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;
【问题讨论】:
-
它是一个非常旧或非常未打补丁的 SQL Server 实例,至少不支持 TLS 1.2?
-
@AlwaysLearning 我正在使用 SQL Server 2019
-
如果您有权访问本地服务器,您是否查看了错误日志中的错误 18456 事件?将报告的 State 值与 MSSQLSERVER_18456 进行比较,看看 SQL Server 认为实际问题是什么。
-
请参阅此answer。您的本地网络可能不允许该端口上的传入连接
标签: sql-server azure azure-functions database-connection hybrid