如何用ADO取得数据库里面所有表单的列表和名字

解决方案 »

  1.   

    ADOX 对象总结对象 说明 
    Catalog 包含描述数据源模式目录的集合。 
    Column 表示表、索引或关键字的列。 
    Group 表示在安全数据库内有访问权限的组帐号。 
    Index 表示数据库表中的索引。 
    Key 表示数据库表中的主关键字、外部关键字或唯一关键字。 
    Procedure 表示存储的过程。 
    Table 表示数据库表,包括列、索引和关键字。 
    User 表示在安全数据库内具有访问权限的用户帐号。 
    View 表示记录或虚拟表的过滤集。 
    使用上面的这些对象,可以满足你的要求。
    #import "c:\program files\common files\system\ado\msadox.dll"rename_namespace("ADONS")rename("EOF","EndOfFile")
    using namespace ADONS;
      

  2.   

    if you are using sql server, you can try
    select name from sysobjects where type = 'U'
    or
    select TABLE_NAME from INFORMATION_SCHEMA.TABLES where table_type = 'BASE TABLE'
      

  3.   

    sfcyyc() ::哥们怎么用呀!!!!
      

  4.   

    //////////////////////////////////////////////////
    //Get field name and length of a table
    /////////////////////////////////////////////////
    _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();
      

  5.   

    http://www.csdn.net/develop/Read_Article.asp?Id=11747 
    枚举所有表
    在VC中使用ADO
    1. 导入ADO类型库
    #pragma warning(disable: 4146)
    #import  "c:\program files\common files\system\ado\msado15.dll"  no_namespace rename( "EOF", "adoEOF" )
    #pragma warning(default: 4146)
    注:在最新的SDK中这句话编译不通过!(???) 2. 增加自动化支持
    在CMyApp::InitInstance()中加入 
    if (!AfxOleInit())
        return FALSE;3 .以CAboutDlg为例,在其中放置一个ListBox控件,对应于m_list4. 在CAboutDlg中声明
    _RecordsetPtr m_pRecordset;
    _ConnectionPtr m_pConnection;5. 在OnInitDialog()中建立连接
    try{
        m_pConnection.CreateInstance(__uuidof(Connetion));
        m_pConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\GCL6\\Data\\Test.mdb;",
            "","",-1); //在"="两边莫加空格!
    catch(...)
    {
        AfxMessageBox("数据库连接失败!");
    }      6. 打开查询,访问数据
    _variant TheValue;
    m_pRecordset.CreateInstance(__uuidof(Recordset));
    m_list.ResetContent();
    try
    {
        m_pRecordset->Open("select * from property",  //注意:m_pRecordset后加 . 和加 -> 功能不同
            m_pConnection.GetInterfacePtr(),
            adOpenDynamic,
            adLockOptimistic,
            adCmdText
            );
        while (!m_pRecordset->adoEOF)
        {
            TheValue = m_pRecordset->GetCollect("Caption");
            if (TheValue.vt != VT_NULL)
                m_list.AddString((char *)_bstr_t(TheValue));
            m_pRecordset -> MoveNext();
        }
        m_pRecordset -> Close();
    }
    catch(...)
    {
        AfxMessageBox("Error!");
    }7. 访问域
    FieldPrt pField;
    for (int i=0; i<m_pRecordset->Fields->Count-1; i++)
    {
        pField = m_pRecordset->Fields->GetItem(long(i)));
    }
    (char *)pField->Name 即域名。
    pField->Type是数据类型
    pField->Precision是精度
    注:数值类型和货币类型的Precision小于255,其余类型都是255。 8. 枚举所有表/查询
    m_pRecordset = m_pConnetion->OpenSchema(adSchemaTable);
    while(!m_pRecordset->adoEOF)
    {
        char pType[40];
        strcpy(pType, (char *)_bstr_t(m_pRecordset->GetCollect("TABLE_TYPE")));
        if (!strcmp(pType, "TABLE"))  //"VIEW"对应于查询
            m_pRecordset -> GetCollect("TABLE_NAME"); //得到表名(需要转化一下)
    }
      

  6.   

    ADOX 2.7 API Reference  See Also
    ADOX API Reference | ADOX Object Model | ADOX Objects | Microsoft ADOX Programmer's Reference&copy; 1998-2001 Microsoft Corporation. All rights reserved.
    ADOX CollectionsColumns Contains all Column objects of a table, index, or key. 
    Groups Contains all stored Group objects of a catalog or user. 
    Indexes Contains all Index objects of a table. 
    Keys Contains all Key objects of a table. 
    Procedures Contains all Procedure objects of a catalog. 
    Tables Contains all Table objects of a catalog. 
    Users Contains all stored User objects of a catalog or group. 
    Views Contains all View objects of a catalog.