The point here is that I don't have the data base table names so please don't suggest to choose one table on to do on it 'SELECT COUNT(*)'
Asked
Active
Viewed 263 times
3
-
5If you don't know the db table names, what do you need to check a connection for? – R. Martinho Fernandes May 15 '11 at 13:14
-
@Martinho Fernandes - I want to create a generic Base Class that manage my connection to the DB. I give it the connection string but to give it a table name just for checking the connection seems exaggerated to me. – Erez May 15 '11 at 13:40
3 Answers
5
This depends on the database, but usually there are some tables that always exist or a table isn't even required.
For Oracle:
SELECT 1 FROM dual
For SqlServer:
SELECT 1
Not very elegant, but generally does the job if you know the database brand.

SirViver
- 2,411
- 15
- 14
-
I disagree with "not very elegant". Ok, the Oracle version is ugly, but that's because of the hack that is `dual`. But `SELECT 1` is extremely elegant. – R. Martinho Fernandes May 15 '11 at 13:18
1
You haven't said what database type it is, but you could use something like DbConnection.GetSchema
which is bound to need a working connection. I don't know how heavy a hit that would be though... if you knew more about the database type there may be a simpler "heartbeat" query you could perform.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
1
You could run a command that doesn't query against a table, such as:
select @@VERSION

Chris Fulstow
- 41,170
- 10
- 86
- 110