Dot net Core – 如何修复:MSSQL 2017 的超时错误(.Net 4.7.1 不会发生)
Dot net Core – How to fix: TimeOut-Error to MSSQL 2017 (which does not happen with .Net 4.7.1)
无法从 dotnet core 2.2 控制台应用程序连接到 MS SQL Server 2017 Express。
已检查 Connection to SQL Server Works Sometimes
中的服务器配置
我安装了新的 Microsoft SQL Server 2017 Express。然后使用控制台应用程序(在 .Net Framework 4.7.1 下)测试与该服务器的连接。有效!
然后我在Dot Net Core 2.2下创建了一个控制台应用程序。安装了 NuGet 包 System.Data.SqlClient 并尝试使用我之前测试过的相同连接字符串连接到 sql 服务器,但出现超时错误。这怎么能解决? (我也用了包Microsoft.Data.SqlClient,结果一样。)
如果我尝试连接到另一个 SQL-服务器 (2008),连接建立没有问题。
using System;
using System.Data.SqlClient;
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Connecting");
using (var conn = new SqlConnection(@"server=<IP>\SQLEXPRESS;user id=sa;password=<PASSWORD>;database="))
{
Console.WriteLine("Try to open connection");
conn.Open();
Console.WriteLine("Connection opened");
}
Console.ReadLine();
}
}
}
发生以下异常:
Microsoft.Data.SqlClient.SqlException: 'Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=21064; handshake=50; '
通过在连接字符串的 server
参数中指定 np:
限定符来强制使用命名管道。
Console.WriteLine("Connecting");
using (var conn = new SqlConnection(@"server=np:<IP>\SQLEXPRESS;user id=sa;password=<PASSWORD>;database="))
{
Console.WriteLine("Try to open connection");
conn.Open();
Console.WriteLine("Connection opened");
}
Console.ReadLine();
尝试捕获 SQLServer 超时异常:
try
{
// some code
}
catch (SqlException ex) when (ex.Number == -2) // -2 is a sql timeout
{
// handle timeout
}
这可能会有所帮助 timeout
要为 SQLExpress 启用远程访问,您必须 Configure Express to accept remote connections
在ConnectionString
中:
- 需要 SQLExpress 服务器端口。
- 参数
database
不能为空。
如果一切都配置良好,修复 timeout
的最简单方法是:
dbConnection.ConnectionTimeout = 0;
这会让ADO.NETtry/wait一次又一次,直到它真的失败。
这是一个很好的例子:
Server=sampleServer\SQLEXPRESS,samplePort;Database=sampleDB;Persist Security Info=True;User ID=sa;Password=12345678;
无法从 dotnet core 2.2 控制台应用程序连接到 MS SQL Server 2017 Express。
已检查 Connection to SQL Server Works Sometimes
中的服务器配置我安装了新的 Microsoft SQL Server 2017 Express。然后使用控制台应用程序(在 .Net Framework 4.7.1 下)测试与该服务器的连接。有效!
然后我在Dot Net Core 2.2下创建了一个控制台应用程序。安装了 NuGet 包 System.Data.SqlClient 并尝试使用我之前测试过的相同连接字符串连接到 sql 服务器,但出现超时错误。这怎么能解决? (我也用了包Microsoft.Data.SqlClient,结果一样。)
如果我尝试连接到另一个 SQL-服务器 (2008),连接建立没有问题。
using System;
using System.Data.SqlClient;
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Connecting");
using (var conn = new SqlConnection(@"server=<IP>\SQLEXPRESS;user id=sa;password=<PASSWORD>;database="))
{
Console.WriteLine("Try to open connection");
conn.Open();
Console.WriteLine("Connection opened");
}
Console.ReadLine();
}
}
}
发生以下异常:
Microsoft.Data.SqlClient.SqlException: 'Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=21064; handshake=50; '
通过在连接字符串的 server
参数中指定 np:
限定符来强制使用命名管道。
Console.WriteLine("Connecting");
using (var conn = new SqlConnection(@"server=np:<IP>\SQLEXPRESS;user id=sa;password=<PASSWORD>;database="))
{
Console.WriteLine("Try to open connection");
conn.Open();
Console.WriteLine("Connection opened");
}
Console.ReadLine();
尝试捕获 SQLServer 超时异常:
try
{
// some code
}
catch (SqlException ex) when (ex.Number == -2) // -2 is a sql timeout
{
// handle timeout
}
这可能会有所帮助 timeout
要为 SQLExpress 启用远程访问,您必须 Configure Express to accept remote connections
在ConnectionString
中:
- 需要 SQLExpress 服务器端口。
- 参数
database
不能为空。
如果一切都配置良好,修复 timeout
的最简单方法是:
dbConnection.ConnectionTimeout = 0;
这会让ADO.NETtry/wait一次又一次,直到它真的失败。
这是一个很好的例子:
Server=sampleServer\SQLEXPRESS,samplePort;Database=sampleDB;Persist Security Info=True;User ID=sa;Password=12345678;