On my Windows box I can list user and system ODBC dsns. For example:
In my code I can connect to a database by using the name of a data source. For example:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("M10-Server-Production");
I would like to obtain the list of data source names, both user and system, filter them to include only the ones suitable for my application, in this example they will start with "M10-Server", then offer the list to the user so the user can select which database to connect to.
How do I get the list of DSNs programmatically?
NB: the app may be run on Windows or Linux so solutions for either or both are welcomed.
On windows, you can use the function
SQLDataSources
to list available System and/or user DSN that are configured on the system. See here for more information: https://msdn.microsoft.com/en-us/library/ms711004%28v=vs.85%29.aspxThis function is also available in unixodbc, but I never worked with unixodbc.
Some compilable and working sample code to do this on windows:
Replace the
SQL_FETCH_FIRST
withSQL_FETCH_FIRST_USER
orSQL_FETCH_FIRST_SYSTEM
to get only user or system dsn entries.Note that you need to link against
odbc32.lib
when building the app.