5

What would be a very fast way to determine if your connectionstring lets you connect to a database?

Normally a connection attempt keeps the user waiting a long time before notifying the attempt was futile anyway.

2
  • What about connecting to the target machine and port over tcp/ip and seeing if theres something listening there. I often telnet to a port just to see if something accepts the connection. Commented Jul 7, 2015 at 4:54
  • that might work, but creates other code than the standard database connection library, plus it doesn't guarantee the database will accept your connection Commented Jul 7, 2015 at 8:33

2 Answers 2

11

You haven't mentioned what database you are connecting to, however. In SQL Server 2005, from .NET, you can specify a connection timeout in your connection string like so:

server=<server>;database=<database>;uid=<user>;password=<password>;Connect Timeout=3

This will try to connect to the server and if it doesn't do so in three seconds, it will throw a timeout error.

Sign up to request clarification or add additional context in comments.

Comments

2

Shorten the timeout on the connection string and execute something trivial.

The wait should be about the same as the timeout.

You would still need a second or two though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.