What is the best way I can test the database connection using C# and Oracle.ManagedDataAccess.Client
?
Should I test before every conn.Open()
to see if the server is accessible or do one test on first run?
My main problem is this: if the database server is offline, I get an exception on the webpage, this is not the best way to interact with the user.
How can I display an error page if the connection couldn't be established?
Example Data Access Layer method:
public bool IsEmailAddressExisting(string emailAddr)
{
using (OracleConnection conn = new OracleConnection(BasicConnection.connectionStringOracle))
using (OracleCommand command = conn.CreateCommand())
{
conn.Open();
command.CommandText = "SELECT id FROM Users WHERE email=:emailAddr";
command.Parameters.Add(":emailAddr", OracleDbType.Varchar2).Value = emailAddr;
var result = command.ExecuteScalar();
return (result != null);
}
}
Note: I don't want to put try-catch around EVERY method's connection.
I found this answer really useful.