// #include <afxdb.h> void CDataDlg::OnButton2() ... { // TODO: Add your control notification handler code here CStringArray strList; ListTables(FALSE, FALSE, strList ); for( int i=0; i< strList.GetSize(); i++) ...{ m_list1.AddString( strList.GetAt( i ) ); } strList.RemoveAll();} // VC编程列举数据库中的表格 BOOL ListTables( bool bViews, bool bSystemTables, CStringArray & listbox) ... { // const char strDSN[]="ds_person";//???¨??μ?ODBCêy?Y?′??3? // CDatabase database; CDatabase m_database; int ret= -1; HSTMT hStmt; UCHAR szName[256]; SDWORD cbName; // è±ê?è?ó??§±í?¢2?êy????êóí??¢?μí3±í CString type = "'TABLE'"; if( bViews ) type += ", 'VIEW'";//êóí? if( bSystemTables ) type += ", 'SYSTEM TABLE'";//?μí3±í // í¨1yODBC′ò?aêy?Y?a try ...{ if( !m_database.Open(NULL,FALSE,FALSE,_T("ODBC;"),TRUE) ) return false; } catch(CDBException * e) ...{ AfxMessageBox(e->m_strError); return false; } // ·???Statement??±ú //SQLAllocStmt (database.m_hdbc,&hStmt); SQLAllocHandle (SQL_HANDLE_STMT,m_database.m_hdbc,&hStmt); // ??è?±íD??¢(·μ??ò???êy?Y±í) ret = SQLTables(hStmt, NULL,SQL_NTS, NULL,SQL_NTS, NULL,SQL_NTS, (unsigned char *)type.GetBuffer(0),SQL_NTS); if(ret == SQL_ERROR) ...{ SQLFreeStmt(hStmt,SQL_CLOSE); m_database.Close(); if(ret == SQL_INVALID_HANDLE ) ...{ AfxMessageBox("Invalid handle"); return false; } AfxMessageBox("Database Could Not be Open"); return false; } // áD±í???t???? // listbox.ResetContent(); CString strtemp; while(1) ...{ strtemp = ""; // è?êy?Yμ??o3??? ret = SQLFetch(hStmt); if(ret == SQL_NO_DATA_FOUND) break; // è??3ò?áDêy?Y(μú3áD?a±í??£??????1óD±íê?D??¢êy?Y?a??μè) // ret = SQLGetData(hStmt, 3, SQL_C_CHAR, szName, TABLE_NAME_LENGTH, &cbName); ret = SQLGetData(hStmt, 3, SQL_C_CHAR, szName, 10, &cbName); strtemp = ((char*)szName);//?óè?áD±í???t??ê? listbox.Add(strtemp); } // listbox.SetCurSel(0);//???D // êí·???±ú SQLFreeStmt( hStmt,SQL_CLOSE); m_database.Close(); return true; } 原 罗纳尔成