用DAO读取access用户表的信息

下面演示了怎么用DAO读某个数据库里面的所有用户表的信息:

 CStringArray tableName_array;
 CStringArray fieldName_array;
 CDaoTableDefInfo table_info;  // 表的信息
 
 // CDaoDatabase db; 在之前已经Open数据库
 int  nAmount = db.GetTableDefCount();
 for ( int nIndex = 0; nIndex < nAmount; nIndex ++ )
 {
  db.GetTableDefInfo( nIndex, table_info );

  // 只关心用户表
  if ( !( table_info.m_lAttributes & dbSystemObject ||    
   table_info.m_lAttributes & dbHiddenObject ) )  
  {  
    // 记录表名
    tableName_array.Add( table_info.m_strName );

    
    // 记录这张表的所有字段名
    // CDaoRecordset RecSet( &db ); 之前已经定义

    CString  strSql = "";
    strSql.Format( "select * from %s", table_info.m_strName );
    RecSet.Open( AFX_DAO_USE_DEFAULT_TYPE, strSql, NULL );

    int  nFieldAmount = RecSet.GetFieldCount( );
    
    CString  strFirstInfo;
    strFirstInfo.Format("%s@%d", table_info.m_strName,

nFieldAmount );
    fieldName_array.Add( strFirstInfo );

    for ( int nFieldPos = 0; nFieldPos < nFieldAmount;
    nFieldPos ++ )
    {
     CDaoFieldInfo fieldInfo; // 字段的信息
     RecSet.GetFieldInfo( nFieldPos, fieldInfo );
     
     fieldName_array.Add( fieldInfo.m_strName );
    }

    RecSet.Close();
  
  }

 
 }

 // trace ok  这里是验证代码
 for ( int nTableNamePos =0; nTableNamePos < tableName_array.GetSize();
   nTableNamePos ++ )
   {
    TRACE( tableName_array.GetAt(nTableNamePos ) + "/n" );
    CString strTableName = tableName_array.GetAt( nTableNamePos );
    for ( int  nFieldPos = 0; nFieldPos < fieldName_array.GetSize();
     nFieldPos ++ )
     {
      CString  strTarget = strTableName + "@";
      CString   strTemp = fieldName_array.GetAt( nFieldPos );
      int  nStartPos = -1;
      if ( (nStartPos = strTemp.Find(strTarget) )     != -1 )
      {
       int  nEndPos = strTemp.Find( "@" );
       int  nFieldAmount = atoi( strTemp.Mid( nEndPos + 1 ) );
       for ( int nFieldOneTablePos = 0; nFieldOneTablePos <   nFieldAmount;  nFieldOneTablePos ++ )
       {
        TRACE( fieldName_array.GetAt(  nFieldPos + 1 + nFieldOneTablePos ) + "/n" );
       }
      }
     }
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值