Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC

A Set of ADOX Classes

Rate me:
Please Sign up or sign in to vote.
4.95/5 (34 votes)
19 Jun 2002CPOL6 min read 941.3K   5.1K   152   210
Simple database catalog access using a set of ADOX classes

Table of Contents

Overview

I created these classes to make it easy to work with ADOX. For this, I created the CADOXCatalog class, the CADOXTable class, the CADOXIndex class, the CADOXProcedure class and the CADOXUser class.

The CADOXCatalog Class

CADOXCatalog::CADOXCatalog

Constructs a CADOXCatalog object:

C++
CADOXCatalog();

CADOXCatalog::Open

The Open function establishes connections to a database:

C++
bool Open(LPCTSTR lpstrConnection);

Parameters

  • LPCTSTR lpstrConnection: A string pointer containing a Connection String.

If the function succeeds, this returns true.

For example:

C++
CADOXCatalog pxcat;
CString strConnection = _T("");

strConnection = "Provider=Microsoft.JET.OLEDB.4.0;Data source = "
                "c:\\adox\\Test.mdb;Jet OLEDB:Engine Type=5;";

pxcat.Open(strConnection);

CADOXCatalog::CreateDatabase

C++
bool CreateDatabase(LPCTSTR lpstrCreate);

Parameters

  • LPCTSTR lpstrCreate

If the function succeeds, this returns true.

For example:

C++
CADOXCatalog pxcat;
CString strConnection = _T("");

strConnection = "Provider=Microsoft.JET.OLEDB.4.0;Data source = "
                "c:\\adox\\Test.mdb;Jet OLEDB:Engine Type=5;";

pxcat.CreateDatabase(strConnection);

CADOXCatalog::AddTable

The AddTable function adds a new table to the database.

C++
bool AddTable(CADOXTable pTable);

Parameters

  • CADOXTable pTable: Pointer to the CADOXTable Object.

If the function succeeds, this returns true.

See sample

CADOXCatalog::AddUser

The AddUser function adds a new user to the database.

C++
bool AddUser(CADOXUser pUser, LPCTSTR lpstrPassword);

Parameters

  • CADOXUser pUser: A pointer to the CADOXUser Object.
  • LPCTSTR lpstrPassword A string pointer containing the user password.

If the function succeeds, this returns true.

CADOXCatalog::GetProcedureCount

The GetProcedureCount function returns the number of stored procedures defined for the database.

C++
long GetProcedureCount();

CADOXCatalog::GetTableCount

The GetTableCount function returns the number of tables defined for the database.

C++
long GetTableCount();

CADOXCatalog::GetViewCount

The GetViewCount function returns the number of fields defined for the database.

C++
long GetViewCount();

CADOXCatalog::GetUserCount

The GetUserCount function returns the number of users that exist in the database.

C++
long GetUserCount();

CADOXCatalog::GetGroupCount

The GetGroupCount function returns the number of groups that exist in the database.

C++
long GetGroupCount();

CADOXCatalog::GetTableName

The GetTableName function obtains the table name defined in the database.

C++
void GetTableName(long nTableIndex, CString strTableName);

Parameters

  • long nTableIndex: The Table Index in the database that is opened by CADOXCatalog
  • CString strTableName: Pointer to a buffer in which to return the Table Name.

CADOXCatalog::GetProcedureName

The GetProcedureName function obtains the stored procedure name defined in the database.

C++
void GetProcedureName(long nProcedureIndex, CString strProcedureName);

Parameters

  • long nProcedureIndex: The Procedure Index in the database that is opened by CADOXCatalog
  • CString strProcedureName: Pointer to a buffer in which to return the Procedure Name.

CADOXCatalog::GetViewName

The GetViewName function obtains the View name defined in the database.

C++
void GetViewName(long nViewIndex, CString strViewName);

Parameters

  • long nViewIndex: The View Index in the database that is opened by CADOXCatalog
  • CString strViewName: Pointer to a buffer in which to return the View Name.

CADOXCatalog::GetUserName

The GetUserName function obtains the User name defined in the database.

C++
void GetUserName(long nUserIndex, CString strUserName);

Parameters

  • long nUserIndex: The User Index in the database that is opened by CADOXCatalog
  • CString strUserName: Pointer to a buffer in which to return the User Name.

CADOXCatalog::GetGroupName>

The GetGroupName function obtains the Group name defined in the database.

C++
void GetGroupName(long nGroupIndex, CString strGroupName);

Parameters

  • long nGroupIndex: The Group Index in the database that is open by CADOXCatalog
  • CString strGroupName: Pointer to a buffer in which to return the Group Name.

CADOXCatalog::DeleteTable

The DeleteTable function deletes the definition of a table in the database.

C++
bool DeleteTable(long nTableIndex);
bool DeleteTable(LPCTSTR lpstrTableName);

Parameters

  • long nTableIndex: The Table Index in the database that is opened by CADOXCatalog
  • LPCTSTR lpstrTableName: A string pointer containing the Table Name.

CADOXCatalog::DeleteProcedure

The DeleteProcedure function deletes the definition of a Stored Procedure in the database.

C++
bool DeleteProcedure(long nProcedureIndex);
bool DeleteProcedure(LPCTSTR lpstrProcedureName);

Parameters

  • long nProcedureIndex: The Procedure Index in the database that is opened by CADOXCatalog
  • LPCTSTR lpstrProcedureName: A string pointer containing the Procedure Name.

CADOXCatalog::DeleteView

The DeleteView function deletes the definition of a View in the database.

C++
bool DeleteView(long nViewIndex);
bool DeleteView(LPCTSTR lpstrViewName);

Parameters

  • long nViewIndex: The View Index in the database that is opened by CADOXCatalog
  • LPCTSTR lpstrViewName: A string pointer containing the View Name.

CADOXCatalog::DeleteGroup

The DeleteGroup function deletes the definition of a Group in the database.

C++
bool DeleteGroup(long nGroupIndex);
bool DeleteGroup(LPCTSTR lpstrGroupName);

Parameters

  • long nGroupIndex: The Group Index in the database that is opened by CADOXCatalog
  • LPCTSTR lpstrGroupName: A string pointer containing the Group Name.

CADOXCatalog::DeleteUser

The DeleteUser function deletes the definition of a User in the database.

C++
bool DeleteUser(long nViewIndex);
bool DeleteUser(LPCTSTR lpstrUserName);

Parameters

  • long nViewIndex: The User Index in the database that is opened by CADOXCatalog
  • LPCTSTR lpstrUserName: A string pointer containing the User Name.

The CADOXTable Class

CADOXTable::CADOXTable

Constructs a CADOXTable object:

C++
CADOXTable(CADOXCatalog* pCat);

Parameters

CADOXCatalog* pCat: Pointer to the CADOXCatalog Object.

CADOXTable::Create

The Create function creates a table, that can be added to the database using CADOXCatalog::AddTable.

C++
bool Create(LPCTSTR lpstrTableName);

Parameters

  • LPCTSTR lpstrTableName: A string pointer containing the Table Name.

See sample

CADOXTable::Open

The Open function opens an existing table in the database.

C++
bool Open(long nTableIndex);
bool Open(LPCTSTR lpstrTableName);

Parameters

  • long nTableIndex: The Table Index in the database that is opened by CADOXCatalog
  • LPCTSTR lpstrTableName: A string pointer containing the Table Name.

CADOXTable::AddField

The AddField function creates a field in the table.

C++
bool AddField(LPCTSTR lpstrFieldName, enum DataType Type, int nLength = 0);

Parameters

  • LPCTSTR lpstrFieldName: A string pointer containing a Field Name.
  • enum DataType Type: A value indicating the data type of the field. The setting can be one of these values:
    • CADOXTable::typeSmallInt
    • CADOXTable::typeInteger
    • CADOXTable::typeUnsignedTinyInt
    • CADOXTable::typeUnsignedSmallInt
    • CADOXTable::typeUnsignedInt
    • CADOXTable::typeUnsignedBigInt
    • CADOXTable::typeSingle
    • CADOXTable::typeDouble
    • CADOXTable::typeCurrency
    • CADOXTable::typeDecimal
    • CADOXTable::typeNumeric
    • CADOXTable::typeBoolean
    • CADOXTable::typeDate
    • CADOXTable::typeDBDate
    • CADOXTable::typeDBTime
    • CADOXTable::typeDBTimeStamp
    • CADOXTable::typeBSTR
    • CADOXTable::typeVarChar
    • CADOXTable::typeLongVarChar
    • CADOXTable::typeWChar
    • CADOXTable::typeVarWChar
    • CADOXTable::typeLongVarWChar
    • CADOXTable::typeBinary
    • CADOXTable::typeVarBinary
    • CADOXTable::typeLongVarBinary
    • CADOXTable::typeChapter
    • CADOXTable::typeFileTime
    • CADOXTable::typePropVariant
    • CADOXTable::typeVarNumeric
  • int nLength = 0: A value that indicates the maximum size, in bytes, of a field. In any kind of fields, nLength is ignored.

See sample

CADOXTable::AddIndex

The AddIndex function creates an Index in the table.

C++
bool AddIndex(CADOXIndex pIndex);

Parameters

CADOXIndex pIndex: Pointer to a CADOXIndex Object.

See sample

CADOXTable::DeleteField

The DeleteField function removes a field and makes it inaccessible.

C++
bool DeleteField(LPCTSTR lpstrFieldName);

Parameters

  • LPCTSTR lpstrFieldName: A string pointer containing the Field Name.

CADOXTable::GetName

C++
void GetName(CString &strTableName);

Parameters

  • CString &strTableName: Reference to a buffer in which to return the Table Name.

The CADOXIndex Class

CADOXIndex::CADOXIndex

Constructs a CADOXIndex object:

C++
CADOXIndex();

CADOXIndex::Create

C++
bool Create(LPCTSTR lpstrIndexName);

Parameters

  • LPCTSTR lpstrIndexName: A string pointer containing a Index Name.

See sample

CADOXIndex::AddField

The AddField function creates a field in the Index.

C++
bool AddField(LPCTSTR lpstrIndexName, enum DataType Type, int nLength = 0);

Parameters

  • LPCTSTR lpstrIndexName: A string pointer containing the Index Name.
  • enum DataType Type: A value indicating the data type of the field. The setting can be one of these values:
    • CADOXIndex::typeSmallInt
    • CADOXIndex::typeInteger
    • CADOXIndex::typeUnsignedTinyInt
    • CADOXIndex::typeUnsignedSmallInt
    • CADOXIndex::typeUnsignedInt
    • CADOXIndex::typeUnsignedBigInt
    • CADOXIndex::typeSingle
    • CADOXIndex::typeDouble
    • CADOXIndex::typeCurrency
    • CADOXIndex::typeDecimal
    • CADOXIndex::typeNumeric
    • CADOXIndex::typeBoolean
    • CADOXIndex::typeDate
    • CADOXIndex::typeDBDate
    • CADOXIndex::typeDBTime
    • CADOXIndex::typeDBTimeStamp
    • CADOXIndex::typeBSTR
    • CADOXIndex::typeVarChar
    • CADOXIndex::typeLongVarChar
    • CADOXIndex::typeWChar
    • CADOXIndex::typeVarWChar
    • CADOXIndex::typeLongVarWChar
    • CADOXIndex::typeBinary
    • CADOXIndex::typeVarBinary
    • CADOXIndex::typeLongVarBinary
    • CADOXIndex::typeChapter
    • CADOXIndex::typeFileTime
    • CADOXIndex::typePropVariant
    • CADOXIndex::typeVarNumeric
  • int nLength = 0: A value that indicates the maximum size, in bytes, of a field. In any kind of fields nLength is ignored

See sample

CADOXIndex::SetPrimaryKey

C++
void SetPrimarKey(bool bPrimary = true);

Parameters

  • bool bPrimary = true

See sample

Sample 01

C++
CADOXTable pxTable(&pxcat);

pxTable.Create("Table4");
pxTable.AddField("Column1", CADOXTable::typeInteger, 0);
pxTable.AddField("Column2", CADOXTable::typeInteger, 0);
pxTable.AddField("Column3", CADOXTable::typeWChar, 25);
pxcat.AddTable(pxTable);

CADOXIndex pxInd;
pxInd.Create("Index1");
pxInd.AddField("Column1", CADOXIndex::typeInteger, 0);
pxInd.SetPrimarKey();
pxTable.AddIndex(pxInd);

The CADOXProcedure Class

CADOXProcedure::CADOXProcedure

Constructs a CADOXProcedure object:

C++
CADOXProcedure(CADOXCatalog* pCat);

Parameters

CADOXCatalog* pCat: Pointer to the CADOXCatalog Object.

CADOXProcedure::GetCommand

C++
void GetCommand(CString strCommand);

Parameters

CString strCommand: Pointer to a buffer in which to return the Command String.

CADOXProcedure::GetName

C++
void GetName(CString strName);

Parameters

CString strName: Pointer to a buffer in which to return the Procedure Name.

CADOXProcedure::Open

C++
bool Open(LPCTSTR lpstrProcedureName);

Parameters

LPCTSTR lpstrProcedureName: A string pointer containing the Procedure Name.

CADOXProcedure::Create

C++
bool Create(CString strName, CString strCommand);

Parameters

  • CString strName: A string pointer containing a Procedure Name.
  • CString strCommand: A string pointer containing the Command String.

The CADOXView Class

CADOXView::CADOXView

Constructs a CADOXView object:

C++
CADOXView(CADOXCatalog* pCat);

Parameters

CADOXCatalog* pCat: Pointer to the CADOXCatalog Object.

CADOXView::GetCommand

C++
void GetCommand(CString strCommand);

Parameters

  • CString strCommand: Pointer to a buffer in which to return the Command String.

CADOXView::GetName

C++
void GetName(CString strName);

Parameters

  • CString strName: Pointer to a buffer in which to return the View Name.

CADOXView::Open

C++
bool Open(LPCTSTR lpstrViewName);

Parameters

  • LPCTSTR lpstrViewName: A string pointer containing the View Name.

CADOXView::Create

C++
bool Create(CString strName, CString strCommand);

Parameters

  • CString strName: A string pointer containing a View Name.
  • CString lpstrCommand: A string pointer containing the Command String.
C++
CString strCommand = _T("Select * From Table4");
xView.Create("View01", strCommand);

The CADOXUser Class

CADOXUser::CADOXUser

Constructs a CADOXUser object:

C++
CADOXUser(CADOXCatalog* pCat);

Parameters

CADOXCatalog* pCat: Pointer to the CADOXCatalog Object.

CADOXUser::GetName

C++
void GetName(CString strName);

Parameters

  • CString strName: Pointer to a buffer in which to return the User Name.

CADOXUser::Create

C++
bool Create(LPCTSTR lpstrUserName);

Parameters

  • LPCTSTR lpstrUserName: A string pointer containing a User Name.

CADOXUser::Open

C++
bool Open(LPCTSTR lpstrUserName);

Parameters

  • LPCTSTR lpstrUserName: A string pointer containing the User Name.

CADOXUser::ChangePassword

C++
bool ChangePassword(LPCTSTR lpstrOldPassword, LPCTSTR lpstrNewPassword);

Parameters

  • LPCTSTR lpstrOldPassword: A string pointer containing the Old Password.
  • LPCTSTR lpstrNewPassword: A string pointer containing the New Password.

Remarks

Some functions do not work in all providers, for example CADOXCatalog::AddTable doesn't work in MS SQL Server.

Special Thanks

Thanks for your ideas, suggestions and collaboration:

  • Christian Graus
  • Mike Osbahr
  • Francis BONTHOUX

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Citigroup
Argentina Argentina
Carlos Antollini is a software engineer working on Object Oriented, Visual C++, MFC, COM, ATL, ADO, Internet technologies and Business Intelligence.
Carlos is originally from Argentina, he was living for several years in Fort Lauderdale, Florida, working for Citibank. Then he started his own business.
Carlos is the creator of <a href="http://www.piFive.com">piFive</a>[<a target="_blank" title="piFive" href="http://www.piFive.com">^</a>], a family of BI Analytic Platform software, that it deals next to, <a href="http://www.latinsys.com">latinsys</a>[<a target="_blank" title="latinsys" href="http://www.latinsys.com">^</a>], his partner in businesses...
Currently he is sharing his passion for project management and BI at Citigroup.

Comments and Discussions

 
AnswerRe: How can I use ADO Classe with ADOX classes Pin
Hrvoje Knezevic28-Jul-03 0:12
Hrvoje Knezevic28-Jul-03 0:12 
Generalproper connection string for jet Pin
rajas13-May-03 14:48
rajas13-May-03 14:48 
GeneralRe: proper connection string for jet Pin
syburn1237-Jul-03 20:08
syburn1237-Jul-03 20:08 
QuestionHow to add a user use adox? Pin
eric_chenwei30-Apr-03 20:30
eric_chenwei30-Apr-03 20:30 
QuestionHow to Use it?? Pin
xxz8-Apr-03 20:42
xxz8-Apr-03 20:42 
AnswerRe: How to Use it?? Pin
Carlos Antollini9-Apr-03 6:00
Carlos Antollini9-Apr-03 6:00 
GeneralRe: How to Use it?? Pin
xxz10-Apr-03 21:58
xxz10-Apr-03 21:58 
GeneralRe: How to Use it?? Pin
syburn1237-Jul-03 19:25
syburn1237-Jul-03 19:25 
QuestionHow to compact a mdb database? Pin
zcy23-Mar-03 19:21
zcy23-Mar-03 19:21 
AnswerRe: How to compact a mdb database? Pin
syburn1237-Jul-03 20:13
syburn1237-Jul-03 20:13 
GeneralRe: How to compact a mdb database? Pin
syburn1237-Jul-03 22:57
syburn1237-Jul-03 22:57 
GeneralDoes this mean that we can't compact the mdb database with the ADO? Pin
zcy8-Jul-03 0:02
zcy8-Jul-03 0:02 
GeneralRe: How to compact a mdb database? Pin
Alfa10-Jul-03 9:16
Alfa10-Jul-03 9:16 
GeneralCompilation Error Pin
Amjad W. Hawash8-Mar-03 20:57
Amjad W. Hawash8-Mar-03 20:57 
GeneralRe: Compilation Error Pin
Carlos Antollini11-Mar-03 5:22
Carlos Antollini11-Mar-03 5:22 
GeneralList of table names in a database Pin
AOUA8-Mar-03 10:33
AOUA8-Mar-03 10:33 
GeneralRe: List of table names in a database Pin
Anonymous10-Mar-03 8:12
Anonymous10-Mar-03 8:12 
GeneralCreating Nullable Fields Pin
Ron de Jong28-Feb-03 15:10
Ron de Jong28-Feb-03 15:10 
GeneralUnhandled Exception when Closing App Pin
ahsile26-Feb-03 9:06
ahsile26-Feb-03 9:06 
GeneralDate Fields question Pin
andruski20-Feb-03 6:28
andruski20-Feb-03 6:28 
GeneralRe: Date Fields question Pin
Ron de Jong28-Feb-03 13:46
Ron de Jong28-Feb-03 13:46 
GeneralPlease help me! I'm confused Pin
larsi197118-Feb-03 22:26
larsi197118-Feb-03 22:26 
GeneralRe: Please help me! I'm confused Pin
Manfred Staiger19-Feb-03 4:11
Manfred Staiger19-Feb-03 4:11 
GeneralRe: Please help me! I'm confused Pin
kittelmann6-Mar-03 23:41
kittelmann6-Mar-03 23:41 
GeneralRe: Please help me! I'm confused Pin
Carlos Antollini7-Mar-03 0:40
Carlos Antollini7-Mar-03 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.