/// <summary> /// Tests a database connection by opening and closing the connection /// </summary> /// <returns>If the connection is valid, otherwise throws an exception</returns> public bool TestConnection() { try { using (IDbConnection connection = DatabaseTypeManager.GetDatabaseConnection(_databaseObjectType)) { connection.ConnectionString = _connectionString; connection.Open(); } return(true); } catch (OracleException oe) { if (oe.Message == "ORA-01017: invalid username/password; logon denied") { throw new InvalidDbCredentialsException("Connection Error: The database username or password is incorrect", oe); } else if (oe.Message == "Connection request timed out") { throw new DbConnectionTimeoutException("Connection Error: Database connection timed out, check your network and/or VPN"); } else { throw; } } catch (Exception) { throw; } }
private T InitConnection <T>(Func <IDbCommand, object> executionFunction, string query, params DParameter[] parameters) { var result = (dynamic)null; try { using (IDbConnection connection = DatabaseTypeManager.GetDatabaseConnection(_databaseObjectType)) { using (IDbCommand command = connection.CreateCommand()) { command.Connection.ConnectionString = _connectionString; command.CommandText = query; command.CommandTimeout = 30; if (parameters != null) // make sure to check, as parameters will not be always passed { foreach (DParameter parameter in parameters) { IDbDataParameter dataParam = command.CreateParameter(); dataParam.ParameterName = parameter.Name; dataParam.Value = parameter.Value; dataParam.DbType = parameter.Type; command.Parameters.Add(dataParam); } } command.Connection.Open(); result = executionFunction(command); } } return(result); } catch (OracleException oe) { if (oe.Message == "ORA-01017: invalid username/password; logon denied") { throw new InvalidDbCredentialsException("Connection Error: The database username or password is incorrect", oe); } else if (oe.Message == "Connection request timed out") { throw new DbConnectionTimeoutException("Connection Error: Database connection timed out, check your network and/or VPN"); } else { log.Error($"Message => {oe.Message}\nStacktrace => {oe.StackTrace}\nQuery => {query}\nParameters => {DParameterHelper.PrepareParameterLog(parameters)}"); throw new DatabaseException(oe.Message, oe); } } catch { throw; } }