点网核心–如何解决: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.
我已经安装了新的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{班级计划{静态void Main(string [] 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:'连接超时已过期.尝试使用登录前握手确认时,超时时间已过.这可能是因为登录前握手失败或服务器无法及时回复.尝试连接到该服务器所花费的时间为-[登录前]初始化= 21064;握手= 50;'
通过在连接字符串的 server
参数中指定 np:
限定符来强制使用命名管道工作.
Console.WriteLine(正在连接");使用(var conn =新的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();
这篇关于点网核心–如何解决:MSSQL 2017的超时错误(.Net 4.7.1不会发生)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!