Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The ado.net with Winform technology found outside the network connection to the database is always prompt: Timeout expired. The timeout period elapsed or the server does not respond before the operation is complete.
sqlconnection resources have been released, and set the time to 0 connectTimeOut useless.

The emergence of new problems
Load data anomalies: System.Data.SqlClient.SqlException (0x80131904): Timeout expired connections. When trying to use the pre-login handshake confirmed that exceeds this timeout. This may be because the pre-login handshake fails or the server failed to respond. Try to connect to the server when the duration is spent - [Pre-Login] initialization = 16; handshake = 14997; ---> System.ComponentModel.Win32Exception (0x80004005): outdated wait operation.

My part of the code:
C#
public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
 {
     SqlConnection.ClearAllPools();
     using (SqlConnection connection = new SqlConnection(connectionString))
     {
         using (SqlCommand cmd = new SqlCommand())
         {
             try
             {
                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                 int rows = cmd.ExecuteNonQuery();
                 cmd.Parameters.Clear();
                 return rows;
             }
             catch (System.Data.SqlClient.SqlException e)
             {
                 SqlConnection.ClearPool(connection);
                 throw e;
             }
         }
     }
 }


 private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
 {
     if (conn.State != ConnectionState.Open)
         conn.Open();
     cmd.Connection = conn;
     cmd.CommandText = cmdText;
     if (trans != null)
         cmd.Transaction = trans;
     cmd.CommandType = CommandType.Text;//cmdType;
     if (cmdParms != null)
     {


         foreach (SqlParameter parameter in cmdParms)
         {
             if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                 (parameter.Value == null))
             {
                 parameter.Value = DBNull.Value;
             }
             cmd.Parameters.Add(parameter);
         }
     }
 }
Posted
Updated 3-Dec-13 15:53pm
v3
Comments
Mike Meinz 3-Dec-13 21:05pm    
You probably have the database server and instance name incorrectly specified.

What does your code look like? Use the Improve Question link above.
Mike Meinz 4-Dec-13 7:47am    
Set a breakpoint at using (SqlConnection connection = new SqlConnection(connectionString)) and tell us the value of connectionstring. Don't assume what it is. Copy it from the Visual Studio Immediate window and paste it into your question using the Improve Question link.

Make sure the SQL Server firewall has Port 1433 configured to allow external connections.
Do you have Persist Security Info=True; in your ConnectionString? If so, change the value to False.
open3820000 10-Dec-13 20:58pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900