3

I am connecting my application with a remote database (SQL Server 2012) and I faced the issue "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=211; handshake=14787;"

Thing to notice is that application is working fine with the remote database but this error occurs only few times a day (e.g. 5 times out of 100 attempts in a day), rest all application is working fine with this database.

Below given is the C# code to connect with the SQL Server database and execute query.

List<string> lstDuplicateSNames = new List<string>();
    string commaSeparatedStudentNames = "X,Y,Z,A,B,C";
                        string query = "SELECT DISTINCT(Student_NAME) FROM V_STUDENT_RECORDS WHERE Student_NAME IN ('" + commaSeparatedStudentNames + "')";
                        using (SqlConnection myConnection = new SqlConnection("Database=Student_DB;Server=nkj-connect.com;Integrated Security=true;"))
                        {
                            using (SqlCommand myCommand = new SqlCommand())
                            {
                                myCommand.Connection = myConnection;
                                myCommand.CommandType = CommandType.Text;
                                myCommand.CommandText = query;
                                myCommand.CommandTimeout = 0;
                                myCommand.Connection.Open();
                                using (SqlDataReader reader = myCommand.ExecuteReader())
                                {
                                    // Check is the reader has any rows at all before starting to read.
                                    if (reader.HasRows)
                                    {
                                        while (reader.Read())
                                        {
                                            lstDuplicateSNames.Add(reader.GetString(0));
                                        }
                                    }
                                }
                            }
                        }

Please suggest what I am missing here.

1 Answers1

0

I have faced scenario similar to this. What worked for me was changing connection server to IPAddress from server name, in your case nkj-connect.com

Prabhat G
  • 2,974
  • 1
  • 22
  • 31