I've got a excel plugin connected to a PostgreSQL database server and all works well. There is a user interface to switch databases, but when somebody accidentally mistypes the database there are a lot of errors - ofcourse. I would like to check on before hand if the server exists and connectable and if the database exists and connectable. I cannot find any reference on the internet on how to do this... Can anybody help me with this?
Asked
Active
Viewed 1,029 times
0
-
Is it possible to instead use a ComboBox with a list of the available databases instead of having the user type it in themselves? This would make a little more sense in my opinion. – Leon Newswanger Jul 03 '14 at 09:30
-
Well... I thought of this but then I have to be able to query for all databases, maybe i could figure that out, but it keeps me still with how to check if the server is ok. I rather check this on before hand, this should be a normal functionality, or am I wrong? – DeniseMeander Jul 03 '14 at 09:33
-
Okay I'm assuming from the dotconnect docs that you have a `PgSqlConnection`, I'll just refer to that as conn. You could simply put `conn.Open()` in a try-catch block, and then alert the user that they've entered an incorrect value. – Leon Newswanger Jul 03 '14 at 09:44
-
Well, yes this is an option, in fact I used it like this but it still feels like a hack... I hoped there would be a method I could call which would just give a boolean back on whether the server was ok, and an other method to check the database. That way it would also be easier to have good user feedback. – DeniseMeander Jul 03 '14 at 10:33
-
1Think of it this way, the only way to check if a sever application is running remotely, is by trying to talk to it. If it responds it's running, if you don't get a response it isn't. Obviously, this is an extremely simplified explanation. When you call `conn.Open()` this is what happens. I personally don't know of a better way to check if an instance of SQL is running in code. – Leon Newswanger Jul 03 '14 at 10:38
-
1From reading the dotConnect documentation this seems to be your best option. If you really want a boolean value you could always make a custom connect method that digests the exception and returns based on that. – Leon Newswanger Jul 03 '14 at 10:46
1 Answers
0
As discussed/proposed by Leon Newswanger, probably the best course to implement a check is to try opening the connection inside a try/catch. I'm maybe to used to PHP where there is a function for every little thing... ;-)
If no one has a better solution I'll mark this one as the solution...

DeniseMeander
- 806
- 2
- 9
- 28