MDAC 2.5 SDK - ADOX


 

Indexes Append Method Example (VC++)

The following code demonstrates how to create a new index. The index is on two columns in the table.

#import "c:\Program Files\Common Files\system\ado\msadox.dll" \
    no_namespace

#include "iostream.h"
#include "stdio.h"
#include "conio.h"

// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void CreateIndexX(void);

//////////////////////////////////////////////////////////
//                                                      //
//      Main Function                                   //
//                                                      //
//////////////////////////////////////////////////////////
void main()
{
    if(FAILED(::CoInitialize(NULL)))
        return; 

    CreateIndexX();

    ::CoUninitialize();
}

//////////////////////////////////////////////////////////
//                                                      //
//        CreateIndexX Function                         //
//                                                      //
//////////////////////////////////////////////////////////
void CreateIndexX(void)
{    
    HRESULT hr = S_OK;

    // Define ADOX object pointers.
    // Initialize pointers on define.
    // These are in the ADOX::  namespace.
    _TablePtr m_pTable   = NULL;
    _IndexPtr m_pIndex    = NULL;
    _CatalogPtr m_pCatalog = NULL;
 
    //Define other variables
    _bstr_t strcnn("Provider=Microsoft.JET.OLEDB.4.0;"
        "Data source = c:\\Program Files\\Microsoft Office\\"
        "Office\\Samples\\Northwind.mdb;");
    try
    {
        TESTHR(hr = m_pTable.CreateInstance(__uuidof(Table)));
        TESTHR(hr = m_pIndex.CreateInstance(__uuidof(Index)));
        TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));

        // Open the catalog.
        m_pCatalog->PutActiveConnection(strcnn);

        // Define the table and append it to the catalog.
        m_pTable->Name = "MyTable";
        m_pTable->Columns->Append("Column1",adInteger,0);
        m_pTable->Columns->Append("Column2",adInteger,0);
        m_pTable->Columns->Append("Column3",adVarWChar,50);
        m_pCatalog->Tables->Append(_variant_t((IDispatch *)m_pTable));

        // Define a multi-column index.
        m_pIndex->Name = "multicolidx";
        m_pIndex->Columns->Append("Column1",adInteger,0);
        m_pIndex->Columns->Append("Column2",adInteger,0);

        // Append the index to the table.
        m_pTable->Indexes->Append(_variant_t((IDispatch *)m_pIndex));

        // Delete the table.
        m_pCatalog->Tables->Delete("MyTable");
    }

    catch(_com_error &e)
    {
        // Notify the user of errors if any.
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());
 
        printf("\n\tSource :  %s \n\tdescription : %s \n ",
             (LPCSTR)bstrSource,(LPCSTR)bstrDescription);
    }

    catch(...)
    {
        cout << "Error occured in include files...."<< endl;
    }
}