MDAC 2.5 SDK - ADOX


 

Columns and Tables Append Methods, Name Property Example (VC++)

See Also

The following code demonstrates how to create a new 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 CreateTableX(void);

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

    CreateTableX();

    ::CoUninitialize();
}

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

        // Define ADOX object pointers.
        // Initialize pointers on define.
        // These are in the ADOX::  namespace.
        _CatalogPtr m_pCatalog = NULL;
        _TablePtr m_pTable = NULL;

        try
        {
            TESTHR(hr = m_pCatalog.CreateInstance(__uuidof(Catalog)));

            //Open the catalog
            m_pCatalog->PutActiveConnection(
                "Provider=Microsoft.Jet.OLEDB.4.0;" \
                "data source=c:\\Program Files\\Microsoft Office"
                "\\Office\\Samples\\Northwind.mdb;");

            TESTHR(hr = m_pTable.CreateInstance(__uuidof(Table)));
            m_pTable->PutName("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));

            //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;
    }
}