Creating Database Objects Using ADOX

Environment: VC6 SP5

This code creates an MDB database with three tables in it using ADOX.

GENERAL

ADOX stands for 'Extended ADO'. It extends ADO in that you can create tables, databases, columns etc. without the use of 'Create Table'-like SQL statements.

STEPS:

  • Use AppWizard to create an MFC-application. Make it dialog based.

  • In AppWizard, be sure to leave 'Include ActiveX Controls' checked.
    (This is apparantly needed to communcicate with ADOX - also true for ADO

  • Paste this statement into your StdAfx.h file:
    #import "C:\Program Files\Common Files\System\ado\msadox.dll" 
        no_namespace rename("EOF", "EndOfFile")
    (Check whether the path is valid for your system)
    (Check other sides at CodeGuru if you want to omit the full path)

  • Make sure you have the following lines in your InitInstance function:
    BOOL CADOXApp::InitInstance()
    {
      AfxEnableControlContainer();
    
      if (!AfxOleInit())
      {
        return FALSE;
      }
    

In your CPP file:

This code supposes the existence of:

  • A dialog called CADOXDlg
  • A button called Createmdb
  • Four editboxes where the user will add MDB Filename + the name of the three tables.



void CADOXDlg::OnCreatemdb()
{
  //Create MDB file with 3 tables, using ADOX! Leuk, leuk, leuk.
  
  char cDBName[80], cCardsTable[80], 
       cLoggingTable[80], cImageTable[80];
 
  //Read contents of Editboxes into their variables:
  UpdateData(TRUE);

  strcpy(cDBName,m_sMDBName);
  strcpy(cCardsTable,m_sTable1);
  strcpy(cLoggingTable,m_sTable2);
  strcpy(cImageTable,m_sTable3,79);

  // Define ADOX object pointers to Catalog (Database),
  //  Table and Column objects.
  _CatalogPtr m_pCatalog  = NULL;
  _TablePtr m_pTable  = NULL;
  _ColumnPtr m_pColumn  = NULL;

  //Create object instances:
  m_pCatalog.CreateInstance(__uuidof (Catalog));
  m_pTable.CreateInstance(__uuidof (Table));

  //Create Database
  m_pCatalog->Create((const char *)(CString(
          "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\") + 
          cDBName + ".mdb"));

  //Create First table
  m_pTable->PutName(cCardsTable);

  m_pTable->Columns->Append("EmpID",adInteger,0); //Third argument is
                                                  //sort of optional. 
                                                  //Enter '0' if not 
                                                  //specified.
  m_pTable->Columns->Append("FirstName",adVarWChar,30);
  m_pTable->Columns->Append("LastName",adVarWChar,30);
  m_pTable->Columns->Append("Address",adVarWChar,24);
  m_pTable->Columns->Append("City",adVarWChar,24);
  m_pCatalog->Tables->Append(_variant_t((IDispatch *)m_pTable));
  m_pTable  = NULL;		//Reset pointer so as to use again
  
  //Create Second table
  m_pTable.CreateInstance(__uuidof (Table));
  m_pTable->PutName(cLoggingTable);

  m_pTable->Columns->Append("ID",adInteger,0);
  m_pTable->Columns->Append("FirstName",adVarWChar,30);
  m_pTable->Columns->Append("LastName",adVarWChar,30);
  m_pTable->Columns->Append("Address",adVarWChar,24);
  m_pTable->Columns->Append("City",adVarWChar,24);
  m_pTable->Columns->Append("ChangedBy",adVarWChar,24);
  m_pCatalog->Tables->Append(_variant_t((IDispatch *)m_pTable));
  m_pTable  = NULL;

  //Create Third table
  m_pTable.CreateInstance(__uuidof (Table));
  m_pTable->PutName(cImageTable);

  m_pTable->Columns->Append("ID",adInteger,0);
  m_pTable->Columns->Append("Length",adInteger,0);
  m_pTable->Columns->Append("Picture",adLongVarBinary,0);
  m_pTable->Columns->Append("Type",adInteger,0);
  m_pCatalog->Tables->Append(_variant_t((IDispatch *)m_pTable));
  m_pTable  = NULL;
}


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds