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: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds