如何获取ACCESS数据库中一张表里的所有字段名
如何获取ACCESS数据库中一张表里的所有字段名
[解决办法]
- C/C++ code
#include <conio.h> #define PAUSE printf("\npress any key to exit"); getch(); #define _WIN32_DCOM #pragma warning(push) #pragma warning(disable:4146) #import "e:\program files\common files\system\ado\MSADO15.DLL" no_namespace rename("EOF", "EndOfFile") #pragma warning(pop) int main(int argc, char* argv[]) { _ConnectionPtr m_pConn; _RecordsetPtr m_pRs; CoInitializeEx(NULL, COINIT_MULTITHREADED); _bstr_t bstrConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" "Data Source=F:\\lylong\\db1.mdb;Persist Security Info=False"; try{ m_pConn.CreateInstance( __uuidof(Connection) ); m_pRs.CreateInstance( __uuidof(Recordset) ); m_pConn->Open( bstrConnStr, "", "", adConnectUnspecified ); m_pRs->Open( "table1", m_pConn.GetInterfacePtr(), adOpenForwardOnly, adLockOptimistic, adCmdTable); long lColumn = m_pRs->Fields->Count; printf("%-12s%-10s%-10s\n", "字段名", "长度", "类型"); for( long i=0; i<lColumn; i++ ) { printf("%-12s%-10d%-10d\n", (char*)m_pRs->Fields->Item[i]->Name, m_pRs->Fields->Item[i]->DefinedSize, m_pRs->Fields->Item[i]->Type); } m_pRs->Close(); m_pConn->Close(); }catch( _com_error e ) { printf( "\n%s Error: %ld\r\n" "%s\r\n" "%s\r\n", (char*)e.Source(), e.Error(), (char*)e.Description(), (char*)e.ErrorMessage() ); m_pRs->Close(); m_pConn->Close(); } CoUninitialize(); PAUSE; return 0; }