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

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds