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: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds