[SOLVED] Reading available ODBC data source names?



  • Is there a possibility to receive all the available odbc data source names, maybe in a QStringList? I mean these sources that are managed within Windows' ODBC-Datasource-Administration...



  • I'm not aware of that kind of API within Qt. However you could use the native ODBC API functions to achieve that.



  • On windows, you can just grab them from the registry, if I remember correctly...



  • Tried it with the ODBC API this way:

    @ SQLHENV henv;
    SQLRETURN retcode;

    SQLWCHAR *dbAliasBuf = (SQLWCHAR * )malloc(255);
    SQLWCHAR dbCommentBuf = (SQLWCHAR)malloc(255);
    SQLSMALLINT *aliasLen = (SQLSMALLINT *)malloc(255);
    SQLSMALLINT *commentLen = (SQLSMALLINT *)malloc(255);

    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    retcode = SQLDataSources(henv, SQL_FETCH_FIRST,
    dbAliasBuf, SQL_MAX_DSN_LENGTH + 1,
    aliasLen, dbCommentBuf,
    255, commentLen);
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    LPCWSTR ad = dbAliasBuf;
    }
    }@

    but unfortunately SQLDataSources returns -1.



  • Solved it myself now, the following Code reads the avaiable odbc data sources and stores them in a QComboBox:

    @ // Read available ODBC-Datasources
    QComboBox *cbDsn = new QComboBox();
    SQLHENV henv;
    SQLRETURN retcode;
    SQLWCHAR *dbAliasBuf = (SQLWCHAR * )malloc(SQL_MAX_DSN_LENGTH + 1);
    SQLWCHAR dbCommentBuf = (SQLWCHAR)malloc(255);
    SQLSMALLINT aliasLen;
    SQLSMALLINT commentLen;
    char ad1 = (char)malloc(SQL_MAX_DSN_LENGTH+1);
    size_t pReturnValue;

    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
    retcode = SQLDataSources(henv, SQL_FETCH_FIRST,
    dbAliasBuf, SQL_MAX_DSN_LENGTH + 1,
    &aliasLen, dbCommentBuf,
    255, &commentLen);
    while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
    wcstombs_s(&pReturnValue,ad1,SQL_MAX_DSN_LENGTH+1,(LPCWSTR) dbAliasBuf,_TRUNCATE);
    cbDsn->addItem(tr(ad1));
    retcode=SQLDataSources(henv, SQL_FETCH_NEXT,
    dbAliasBuf, SQL_MAX_DSN_LENGTH + 1,
    &aliasLen, dbCommentBuf,
    255, &commentLen);
    }
    if ( retcode == SQL_ERROR ) {
    SQLWCHAR *SQLState = (SQLWCHAR * )malloc(6);
    SQLINTEGER NativeError;
    SQLWCHAR *MessageText = (SQLWCHAR *)malloc(255);
    SQLSMALLINT TextLength;
    retcode = SQLGetDiagRec(SQL_HANDLE_ENV, henv, 1, SQLState, &NativeError, MessageText, 255, &TextLength);
    }
    }@

    Thread can be closed...



  • You might be better off to separate the reading of the source names, and the putting them into a QComboBox. Better for code-reusability.

    Note that the code above will not work: at line 2 you call your QComboBox cbdsn, while at line 21 it has become cbDsn. What's more, I don't think your string will be translatable, so I doubt the tr() on line 21 will be doing anything useful.



  • I know, i added line 2 manually to make sure what cbDsn is...just corrected this.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.