_Connection dbcon;
_Recordset rs;
dbcon.CreateDispatch("ADODB.Connection");
dbcon.Open(m_strConnection,"","",NULL);
rs.CreateDispatch("ADODB.Recordset");
COleVariant vt(m_strTable);
rs.Open(vt,COleVariant(m_strConnection), adOpenStatic,adLockOptimistic, adCmdTable);
LPDISPATCH lpDisp;
lpDisp=rs.GetFields();
ASSERT(lpDisp);
Fields fields;
fields.AttachDispatch(lpDisp);
VARIANT varIndex;
VariantInit(&varIndex);
varIndex.vt=VT_I2;
int nFields=fields.GetCount();
Field field;
CString strFieldName,strFieldType;
long lDefineSize;
for(int i=0;i<nFields;i++)
{
varIndex.iVal=i;
lpDisp=fields.GetItem(varIndex);
field.AttachDispatch(lpDisp);
strFieldName=field.GetName();
lDefineSize=field.GetDefinedSize();
}
field.ReleaseDispatch();
fields.ReleaseDispatch();
rs.Close();
rs.ReleaseDispatch();
dbcon.Close();
dbcon.ReleaseDispatch();

解决方案 »

  1.   

    #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", "×&Ouml;&para;&Icirc;&Atilde;&ucirc;", "&sup3;¤&para;&Egrave;", "&Agrave;à&ETH;&Iacute;");
    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;
    }
      

  2.   

    乱码为:
    printf("%-12s%-10s%-10s\n", "字段名", "长度", "类型");:)
      

  3.   

    try
    {
    if(m_pDB.IsOpen())
    m_pDB.Close();
    m_pDB.Open(GetDefaultDBName(),FALSE,FALSE,MYPASSWORD);
    }
    catch(CDaoException* e)
    {
    DisplayDaoException(e);
    e->Delete();
    return;
    }
    CDaoTableDef m_OpenTable(&m_pDB);
    try
    {
    m_OpenTable.Open(this->m_strUniqueName);//确定打开什么表
    }
    catch(CDaoException* e)
    {
    DisplayDaoException(e);
    e->Delete();
    return;
    }
    int nFields;
    CDaoFieldInfo fieldInfo;
    try
    {
    nFields=m_OpenTable.GetFieldCount();
    for(int j=0;j<nFields;j++)
    {
    m_OpenTable.GetFieldInfo(j,fieldInfo);
    // rListCtrl.InsertItem( j, fieldInfo.m_strName);//行头
    rListCtrl.InsertColumn( j,fieldInfo.m_strName , LVCFMT_LEFT, 200, 0 );
    }
    }
    catch(CDaoException* e)
    {
    DisplayDaoException(e);
    e->Delete();
    return;
    }
    m_OpenTable.Close();

    LoadColumnWidths(); //查询
      

  4.   

    类型定义:
    enum DataTypeEnum
    {
        adEmpty = 0,
        adTinyInt = 16,
        adSmallInt = 2,
        adInteger = 3,
        adBigInt = 20,
        adUnsignedTinyInt = 17,
        adUnsignedSmallInt = 18,
        adUnsignedInt = 19,
        adUnsignedBigInt = 21,
        adSingle = 4,
        adDouble = 5,
        adCurrency = 6,
        adDecimal = 14,
        adNumeric = 131,
        adBoolean = 11,
        adError = 10,
        adUserDefined = 132,
        adVariant = 12,
        adIDispatch = 9,
        adIUnknown = 13,
        adGUID = 72,
        adDate = 7,
        adDBDate = 133,
        adDBTime = 134,
        adDBTimeStamp = 135,
        adBSTR = 8,
        adChar = 129,
        adVarChar = 200,
        adLongVarChar = 201,
        adWChar = 130,
        adVarWChar = 202,
        adLongVarWChar = 203,
        adBinary = 128,
        adVarBinary = 204,
        adLongVarBinary = 205,
        adChapter = 136,
        adFileTime = 64,
        adPropVariant = 138,
        adVarNumeric = 139,
        adArray = 8192
    };