相关文章推荐
英俊的大熊猫  ·  TIBCO ...·  1 年前    · 
阳刚的凉面  ·  VS2017 ...·  2 年前    · 
We are using windows application in c#.NET 4.0 with SQL server 2005. We got the Timeout expired exception while establishing the connection to the SQL server.Can anyone know the reason why we are getting this type of exception.
Stack Trace is as follows:
System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error) at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket() at System.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean& marsCapable) at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open()
Thanks in Advance. Did you set the CommandTimeout ? . If not can you set the CommmandTimeout and check ?
The below url will give you more information about CommandTimeout.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx [ ^ ] It is not advisable to set the value to zero. You set some value after checking the execution time of your biggest query. If your biggest query takes 10 minutes to execute , set the value to '600'. If you have 1000 queries, and out of that one query takes 20 mints and all others takes 1 mint, what i do is set the value to '60' and identify the particular query, if that particular query is going to execute, i will set the value to '20 * 60'.
Thank you for your response. Unfortunatly the above exception is raised while establishing the connection to server [i.e., conn.Open() ] but not from command object.
My code is look like below:

SqlCommand cmdSqlCommand = new SqlCommand();

cmdSqlCommand.Connection = Connection; // exception is raised form here. Here
Connection is the Public property.

cmdSqlCommand.CommandType = CommandType.StoredProcedure;

cmdSqlCommand.CommandText = StoredProcedureName;

int result = cmdSqlCommand.ExecuteNonQuery();// exception is not raised from here


//"Connection" Property

public SqlConnection Connection
{
get
{
if (conn == null)
{
string connstr = "server=localhost\SQLEXPRESS;Initial Catalog=MYDB;Integrated Security=True";
conn = new SqlConnection(connstr);
}

if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}

return conn;
}
}
Can you give me the solution.
Thanks
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •