Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast database access test from .NET

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.

like image 266
Jorrit Reedijk Avatar asked Aug 14 '08 09:08

Jorrit Reedijk


2 Answers

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.

like image 142
GateKiller Avatar answered Sep 28 '22 01:09

GateKiller


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.

like image 24
Keith Avatar answered Sep 28 '22 03:09

Keith