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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Companies must routinely transfer files and share data to run their business, work with partners, and speed operations. However, many find the traditional approach to file transfer lacks necessary security, is too complex and difficult to manage, does not support the levels of automation needed, and breaks down when addressing the file transfer requirements of new areas like Big Data analytics and mobile applications. This QuinStreet SmartSelect discusses how the changing business environment is making the use …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds