Dot net Core – How to fix: TimeOut-Error to MSSQL 2017 (which does not happen with .Net 4.7.1)

笑着哭i 提交于 2021-02-10 14:18:23

问题


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; '

回答1:


try to catch SQLServer timeout exceptions :

      try
        {
            // some code
        }
        catch (SqlException ex) when (ex.Number == -2)  // -2 is a sql timeout
        {
            // handle timeout
        }

this might hep timeout




回答2:


To enable remote access for SQLExpress, you have to Configure Express to accept remote connections

In ConnectionString:

  1. need a port for SQLExpress server.
  2. parameter database should not be empty.

If have everything well configured, the easiest way to fix timeout is:

dbConnection.ConnectionTimeout = 0;

this will let ADO.NET try/wait again and again, until it really fails.

Here is a good example:

Server=sampleServer\SQLEXPRESS,samplePort;Database=sampleDB;Persist Security Info=True;User ID=sa;Password=12345678;



回答3:


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();



来源:https://stackoverflow.com/questions/56663136/dot-net-core-how-to-fix-timeout-error-to-mssql-2017-which-does-not-happen-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!