I don't think there is a good way to change the database connection in Moodle. However, you can create additional connections that are independent of the Moodle database connection.
You can find very good examples in the core plugins "auth_db" and "enrol_database".
auth_db: in lib.php the function "db_init()"
enrol_database: auth.php the method "db_init()"
In both cases you have an additional connection that you can use to do your requests to the external database.
I hope that helps a little.