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.
检查服务器配置,如连接到 SQL Server 有时工作>
我安装了新的 Microsoft SQL Server 2017 Express.然后使用控制台应用程序(在 .Net Framework 4.7.1 下)测试与该服务器的连接.有效!
然后我在 Dot Net Core 2.2 下创建了一个控制台应用程序.安装了 NuGet 包 System.Data.SqlClient 并尝试使用我之前测试过的相同连接字符串连接到 sql 服务器,但出现超时错误.如何解决这个问题?(我也使用了 Microsoft.Data.SqlClient 包,结果相同.)
如果我尝试连接到另一个 SQL-Server (2008),则连接建立没有问题.
使用系统;使用 System.Data.SqlClient;命名空间 ConsoleClient{课程计划{静态无效主(字符串 [] args){Console.WriteLine("连接");使用 (var conn = new SqlConnection(@"server=<IP>SQLEXPRESS;user id=sa;password=<PASSWORD>;database=")){Console.WriteLine("尝试打开连接");conn.Open();Console.WriteLine("连接打开");}Console.ReadLine();}}}
发生以下异常:
Microsoft.Data.SqlClient.SqlException: '连接超时.尝试使用登录前握手确认时超时时间已过.这可能是因为登录前握手失败或服务器无法及时响应.尝试连接到此服务器所花费的持续时间是 - [Pre-Login] 初始化 = 21064;握手=50;'
通过在连接字符串的 server
参数中指定 np:
限定符来强制使用命名管道工作.
Console.WriteLine("连接");使用 (var conn = new SqlConnection(@"server=np:<IP>SQLEXPRESS;user id=sa;password=<PASSWORD>;database=")){Console.WriteLine("尝试打开连接");conn.Open();Console.WriteLine("连接打开");}Console.ReadLine();
Can't connect to a MS SQL Server 2017 Express from dotnet core 2.2 console application.
Checked Server Configuration as in Connection to SQL Server Works Sometimes
I have installed a new Microsoft SQL Server 2017 Express. Then tested the connection to this server with a console application (under .Net Framework 4.7.1). Works!.
Then I created a console application under Dot Net Core 2.2. Installed NuGet package System.Data.SqlClient and tried connect to the sql server using the same connection string I tested before and got a timeout error. How can this be fixed? (I also used the package Microsoft.Data.SqlClient, with the same result.)
If I try to connect to another SQL-Server (2008) the connection is established without problems.
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();
}
}
}
Following Exception occured:
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; '
Forcing to use named pipes by specifying np:
qualifier in the server
parameter in the connection string does the job.
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();
这篇关于Dot net Core – 如何修复:MSSQL 2017 的超时错误(.Net 4.7.1 不会发生这种情况)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!