Create Method Example (VC++)

Microsoft ActiveX Data Objects (ADO)

Create Method Example (VC++)

The following code shows how to create a new Microsoft Jet database with the Create method.

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

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

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

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

    CreateDatabaseX();
        
    ::CoUninitialize();
}

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

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

    
    //Set ActiveConnection of Catalog to this string
    _bstr_t strcnn("Provider=Microsoft.JET.OLEDB.4.0;"
                "Data source = c:\\new.mdb");
    try
    {
        TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));
        m_pCatalog->Create(strcnn);

    }    

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

}