SAMPLE: Dynamic Column Binding With MFC Database ClassesID: Q117137
|
The DYNCOL sample demonstrates how to dynamically determine the number, types, and names of each column in a given table and then bind these columns to dynamically allocated objects in your CRecordset derived class.
The following file is available for download from the Microsoft Software Library:
DYNCOL.EXEFor more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online ServicesDYNCOL.EXE contains many files, so you will want to preserve the directory structure. After you download it, place it in an empty directory and extract the files it contains by using the -d option:
DYNCOL.EXE -d
DYNCOL follows the basic procedures outlined in the documentation titled,
"Dynamically Binding Data Columns", found in Database Encyclopedia of the
Books OnLine. However, DYNCOL has no permanent members, therefore only one
list is generated that contains information for all the columns in the
table. If you have a subset of columns that are always present and are
therefore member variables of the CRecordset derived class, you will need
to follow the instructions in the article on generating only a list of the
columns not already bound. DYNCOL only constructs a single list and
implements methods for performing DoFieldExchange() and DoDataExchange()
for this list of dynamically allocated objects.
BOOL CDynaSet::Open(UINT nOpenType, LPCSTR lpszSQL, DWORD dwOptions)
{
// Specify the table to look at
m_Columns.m_strTableNameParam = "DYNABIND_SECTION";
// Set the database to be the CDynaSet's database if one hasn't
// already been set (this should be the case)
if (m_Columns.m_pDatabase == NULL)
m_Columns.m_pDatabase = m_pDatabase;
// Open the recordset to get the column info
if (!m_Columns.Open(CRecordset::forwardOnly, NULL,
CRecordset::readOnly))
return FALSE;
// Initialize the number of fields dynamically allocated to CDynaSet
m_nFields = 0;
// Loop until we've seen all the columns
while (!m_Columns.IsEOF())
{
// Allocate a new CColumnData object for the current column
CColumnData *pData = new CColumnData;
// Store the column information
pData->m_nDataType = m_Columns.m_nDataType;
pData->m_strColumnName = m_Columns.m_strColumnName;
// Allocate an object of the appropriate type to store
// the column data
switch(pData->m_nDataType)
{
case SQL_CHAR:
pData->m_pData = (void *)new CString;
break;
case SQL_SMALLINT:
pData->m_pData = (void *)new int;
break;
case SQL_TIME: // Fall through
case SQL_DATE: // Fall through
case SQL_TIMESTAMP:
pData->m_pData = (void *)new CTime;
break;
default:
break;
}
// Add the column descriptor to the list and
// increment the number of columns in the CDynaSet
m_pList.AddTail(pData);
m_nFields++;
// Get the next column's information
m_Columns.MoveNext();
}
// Free the HSTMT used to get the table info
RETCODE nRetCode;
AFX_SQL_SYNC(::SQLFreeStmt(m_Columns.m_hstmt, SQL_CLOSE));
// Return the base class if we got this far
return CRecordset::Open(nOpenType, lpszSQL, dwOptions);
}
void CDynaSet::DoFieldExchange(CFieldExchange* pFX)
{
// Set the type of exchange; same as AppWizard generated
pFX->SetFieldType(CFieldExchange::outputColumn);
// Get a pointer to the first CColumnData object in the list
POSITION rPos = m_pList.GetHeadPosition();
CColumnData *pData = (CColumnData *)m_pList.GetNext(rPos);
// Loop until we've traversed all the columns
while (pData)
{
// Call the appropriate RFX routine for the column's type
switch(pData->m_nDataType)
{
case SQL_CHAR:
RFX_Text(pFX, pData->m_strColumnName,
*((CString *)(pData->m_pData)));
break;
case SQL_SMALLINT:
RFX_Int(pFX, pData->m_strColumnName,
*((int *)(pData->m_pData)));
break;
case SQL_TIME: // Fall through
case SQL_DATE: // Fall through
case SQL_TIMESTAMP:
RFX_Date(pFX, pData->m_strColumnName,
*((CTime *)(pData->m_pData)));
break;
default:
break;
}
// Set pData to NULL if that was the last column
if (rPos)
pData = (CColumnData *)m_pList.GetNext(rPos);
else
pData = NULL;
}
}
void CDynaView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDynaView)
DDX_VBControl(pDX, IDC_GRID, m_vbxGrid);
//}}AFX_DATA_MAP
CPtrList *pList = &m_pSet->m_pList;
// Get the first element
POSITION rPos = pList->GetHeadPosition();
CColumnData *pData = (CColumnData *)pList->GetNext(rPos);
// Allocate a string buffer to be used in conversions
CString strBuffer;
// Set the initial row to fill in
int row = 1;
// Set the GRID to have the correct number of rows
m_vbxGrid->SetNumProperty("Rows", m_pSet->m_nFields + 1);
// Loop until we've traversed all the CColumnData objects
while (pData)
{
// Insert the column name in the first column and move
// to the second column to insert the data
m_vbxGrid->SetNumProperty("Row", row);
m_vbxGrid->SetNumProperty("Col", 1);
m_vbxGrid->SetStrProperty("Text", pData->m_strColumnName);
m_vbxGrid->SetNumProperty("Col", 2);
// Call the appropriate DDX
switch(pData->m_nDataType)
{
case SQL_CHAR:
m_vbxGrid->SetStrProperty("Text", *((CString *)(pData->m_pData)));
break;
case SQL_SMALLINT:
{
// Convert the int to a string
char *pBuffer = strBuffer.GetBuffer(32);
_itoa(*((int *)(pData->m_pData)), pBuffer, 10);
}
strBuffer.ReleaseBuffer();
m_vbxGrid->SetStrProperty("Text", strBuffer);
break;
case SQL_TIME: // Fall through
case SQL_DATE: // Fall through
case SQL_TIMESTAMP:
// Convert the CTime to a string
strBuffer = ((CTime *)(pData->m_pData))->Format( "%c" );
m_vbxGrid->SetStrProperty("Text", strBuffer);
break;
default:
break;
}
// Increment the row and check to see if that was our
// last CColumnData in the list
row++;
if (rPos)
pData = (CColumnData *)pList->GetNext(rPos);
else
pData = NULL;
}
}
The most fundamental concepts of the sample are contained within the
CColumns and CColumnData objects. The first allows DYNCOL to determine the
name and data type for each column in the table. With this information it
is able to allocate a linked list of CColumnData objects. Each CColumnData
object is a descriptor that contains the name, type and a pointer to a
data object of the correct type. Once this list has been constructed,
traversal routines must be put in the DoFieldExchange() and
DoDataExchange() routines to perform the appropriate RFX/DDX function with
the allocated data object.
Additional query words: kbinf ODBC dynamic column bind
Keywords : kb16bitonly kbDatabase kbMFC kbODBC kbVC
Version : 1.50
Platform : WINDOWS
Issue type :
Last Reviewed: August 3, 1999