Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Reading available ODBC data source names?

[SOLVED] Reading available ODBC data source names?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EukeSnud
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • E Offline
          E Offline
          EukeSnud
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            EukeSnud
            wrote on last edited by
            #5

            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...

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                EukeSnud
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved