Connection timeout set to 5 sec but takes longer to timeout



Hi all,

My Connection string timeout was set to 5 sec but commands takes much longer
to timeout when there is no connection to the destination ip. Is the
timeout value only helps when the ip is reachable but the sql server is not,
or it should work also if the ip address is unreachable. If it should work
also when the ip is unreachable, why it does not work in my case?

Thanks a million,


public static int ExecCommand(string command, string ip)


if (String.IsNullOrEmpty(ip))

throw new ManualRedundancyException("IP address for database connection
cannot be null");

string connectionStr = "Data Source=" + ip + ";Initial
Catalog=master;Persist Security Info=True;User
ID=sa;Password=1210;Connection Timeout=5;";

using (SqlConnection cn = new SqlConnection(connectionStr))


using (SqlCommand cm = new SqlCommand(command, cn))



int i = cm.ExecuteNonQuery();

return i;





Thanks for the reply.

I'm only talking to the connection timeout. Event when I set the timeout in
the connection string to 5 seconds it will take much longer to get timeout
if the IP address in unreachable.



If I remember a similar thread you have a limit anyway under which you can't

How much time does it take now ? If I remember you can't go between 10/15 s.
If still higher it could be perhaps another problem (name resolution being
slow ?)

Connect via an IP address

If you try it with the "most anal" connection string....

where you specify the
Protocol ("Network Library" in the above URL)

(And don't forget to throw in the Connection Timeout as well (as you are
doing in your original sample).
Connection Timeout=30";

Which behavior does it exhibit?


And have you verified the connection string value is being written?
(This is a long shot I realize......but better safe than sorry)

using (SqlConnection cn = new SqlConnection(connectionStr))
Console.WriteLine("Just making super sure : " +
Convert.ToString(cn.ConnectionTimeout); // << What are you getting here?
//your other code

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question