read table of Access into CString array via MFC
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include <iostream>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif
#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include
<afxdtctl.h>
// MFC support
for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include
<afxcmn.h>
// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
// tmpmfcconsole.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "tmpmfcconsole.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <afxdb.h>
// The one and only application object
CWinApp theApp;
using namespace std;
class CDBVariantEx : public CDBVariant
{
public:
void GetStringValue(LPSTR lpsz);
void GetStringValue(CString& rstrValue);
};
void CDBVariantEx::GetStringValue(LPSTR lpsz)
{
switch(m_dwType)
{
case DBVT_STRING:
sprintf(lpsz, "%s", m_pstring->GetBuffer(m_pstring->GetLength()));
break;
case DBVT_LONG:
sprintf(lpsz, "%ld", m_lVal);
break;
case DBVT_DOUBLE:
sprintf(lpsz, "%f", m_dblVal);
break;
case DBVT_SHORT:
sprintf(lpsz, "%d", m_iVal);
break;
case DBVT_NULL:
sprintf(lpsz, "%s", _T("NULL"));
break;
case DBVT_BOOL:
if (TRUE == m_boolVal) sprintf(lpsz, "%s", _T("True"));
else sprintf(lpsz, "%s", _T("False"));
break;
case DBVT_DATE:
{
sprintf(lpsz,"%d-%02d-%02d
%02d:%02d:%02d.%03d",m_pdate->year,m_pdate->month,
m_pdate->day,m_pdate->hour,m_pdate->minute,m_pdate->second,m_pdate->fraction);
}
break;
case DBVT_ASTRING:
{
strcpy(lpsz, *m_pstringA);
break;
}
default:
sprintf(lpsz, _T("type:%d"),m_dwType);
break;
}
}
void CDBVariantEx::GetStringValue(CString& rstrValue)
{
switch(m_dwType)
{
case DBVT_STRING:
rstrValue.Format("%s",
m_pstring->GetBuffer(m_pstring->GetLength()));
break;
case DBVT_LONG:
rstrValue.Format("%ld", m_lVal);
break;
case DBVT_DOUBLE:
rstrValue.Format("%f", m_dblVal);
break;
case DBVT_SHORT:
rstrValue.Format("%d", m_iVal);
break;
case DBVT_NULL:
rstrValue.Format("%s", _T(""));
break;
case DBVT_BOOL:
if (TRUE == m_boolVal) rstrValue.Format("%s", _T("True"));
else rstrValue.Format("%s", _T("False"));
break;
case DBVT_DATE:
{
rstrValue.Format("%d-%02d-%02d
%02d:%02d:%02d.%03d",m_pdate->year,m_pdate->month,
m_pdate->day,m_pdate->hour,m_pdate->minute,m_pdate->second,m_pdate->fraction);
}
break;
default:
rstrValue.Format(_T("type:%d"),m_dwType);
break;
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
//sample code to read MDB table into CString array
//build by Onega(www.fruitfruit.com)
//VC++ 2003, Windows XP, Access 2003
//warning: use it at your own risk.
//More error checking, TCHAR handling is required
const int NUMCOLUMNS = 8;
const int MAXNUMRECORDS = 8;
CString strAccessTable[MAXNUMRECORDS][NUMCOLUMNS] ;
CDatabase db;
db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\onega.mdb;");
CRecordset rs(&db);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select * From table1");
int nfieldcount=rs.GetODBCFieldCount();
CString msg;
msg.Format("There are %d fields in table1",nfieldcount);
AfxMessageBox(msg);
int row = 0;
while(!rs.IsEOF())
{
for(short i=0;i<nfieldcount;i++)
{
CDBVariantEx dbvar;
rs.GetFieldValue(i,dbvar);
char buf[1024] = {0};
dbvar.GetStringValue(buf);
strAccessTable[row][i] = buf;
printf("%02d,%02d
%s\n",row,i,strAccessTable[row][i]);
}
rs.MoveNext();
row++;
if(row>=MAXNUMRECORDS)
break;
}
rs.Close();
db.Close();
}
system("pause");
return nRetCode;
}
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include <iostream>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif
#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include
<afxdtctl.h>
// MFC support
for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include
<afxcmn.h>
// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
// tmpmfcconsole.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "tmpmfcconsole.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <afxdb.h>
// The one and only application object
CWinApp theApp;
using namespace std;
class CDBVariantEx : public CDBVariant
{
public:
void GetStringValue(LPSTR lpsz);
void GetStringValue(CString& rstrValue);
};
void CDBVariantEx::GetStringValue(LPSTR lpsz)
{
switch(m_dwType)
{
case DBVT_STRING:
sprintf(lpsz, "%s", m_pstring->GetBuffer(m_pstring->GetLength()));
break;
case DBVT_LONG:
sprintf(lpsz, "%ld", m_lVal);
break;
case DBVT_DOUBLE:
sprintf(lpsz, "%f", m_dblVal);
break;
case DBVT_SHORT:
sprintf(lpsz, "%d", m_iVal);
break;
case DBVT_NULL:
sprintf(lpsz, "%s", _T("NULL"));
break;
case DBVT_BOOL:
if (TRUE == m_boolVal) sprintf(lpsz, "%s", _T("True"));
else sprintf(lpsz, "%s", _T("False"));
break;
case DBVT_DATE:
{
sprintf(lpsz,"%d-%02d-%02d
%02d:%02d:%02d.%03d",m_pdate->year,m_pdate->month,
m_pdate->day,m_pdate->hour,m_pdate->minute,m_pdate->second,m_pdate->fraction);
}
break;
case DBVT_ASTRING:
{
strcpy(lpsz, *m_pstringA);
break;
}
default:
sprintf(lpsz, _T("type:%d"),m_dwType);
break;
}
}
void CDBVariantEx::GetStringValue(CString& rstrValue)
{
switch(m_dwType)
{
case DBVT_STRING:
rstrValue.Format("%s",
m_pstring->GetBuffer(m_pstring->GetLength()));
break;
case DBVT_LONG:
rstrValue.Format("%ld", m_lVal);
break;
case DBVT_DOUBLE:
rstrValue.Format("%f", m_dblVal);
break;
case DBVT_SHORT:
rstrValue.Format("%d", m_iVal);
break;
case DBVT_NULL:
rstrValue.Format("%s", _T(""));
break;
case DBVT_BOOL:
if (TRUE == m_boolVal) rstrValue.Format("%s", _T("True"));
else rstrValue.Format("%s", _T("False"));
break;
case DBVT_DATE:
{
rstrValue.Format("%d-%02d-%02d
%02d:%02d:%02d.%03d",m_pdate->year,m_pdate->month,
m_pdate->day,m_pdate->hour,m_pdate->minute,m_pdate->second,m_pdate->fraction);
}
break;
default:
rstrValue.Format(_T("type:%d"),m_dwType);
break;
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
//sample code to read MDB table into CString array
//build by Onega(www.fruitfruit.com)
//VC++ 2003, Windows XP, Access 2003
//warning: use it at your own risk.
//More error checking, TCHAR handling is required
const int NUMCOLUMNS = 8;
const int MAXNUMRECORDS = 8;
CString strAccessTable[MAXNUMRECORDS][NUMCOLUMNS] ;
CDatabase db;
db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\onega.mdb;");
CRecordset rs(&db);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select * From table1");
int nfieldcount=rs.GetODBCFieldCount();
CString msg;
msg.Format("There are %d fields in table1",nfieldcount);
AfxMessageBox(msg);
int row = 0;
while(!rs.IsEOF())
{
for(short i=0;i<nfieldcount;i++)
{
CDBVariantEx dbvar;
rs.GetFieldValue(i,dbvar);
char buf[1024] = {0};
dbvar.GetStringValue(buf);
strAccessTable[row][i] = buf;
printf("%02d,%02d
%s\n",row,i,strAccessTable[row][i]);
}
rs.MoveNext();
row++;
if(row>=MAXNUMRECORDS)
break;
}
rs.Close();
db.Close();
}
system("pause");
return nRetCode;
}
0 Comments:
Post a Comment
<< Home