[SOLVED] Reading available ODBC data source names?
-
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.