ADO 'ATL OLEDB' DAO ODBC, 속도 비교
#include "StdAfx.h"
/*
//DAO Direct -> MS Office에 종속 ->문제 있음 ,
//#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\ACEDAO.dll" rename( "EOF", "AdoNSEOF" ) //*.accdb 지원
// Microsoft Jet 4.0 sp8 설치
// 없으면 안됨, #include "afxdao.h"보다 먼저와야 한다. import와 /MP 옵션을 함께 사용불가
#import "C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll" rename("EOF", "EndOfFile")//*.mdf 지원 ,
#include <iostream>
using namespace std;
//DAO
//#include "afxdao.h" //CDaoDatabase - DAO Direct 와 충돌
#pragma warning (disable:4995)//CDaoDatabase
//ADO Cpp, MDAC2.8 sp1 설치
#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" rename( "EOF", "AdoNSEOF" )
//C:\Program Files\Common Files\System\ADO\msado25.tlb msado15.dll
using namespace ADODB;
//ODBC
#include "afxdb.h"// CDatabase //ODBC
#include <sqlext.h>//ODBC Direct
//#pragma comment( lib, "odbc32" )//ODBC
//#pragma comment( lib, "odbccp32" )//ODBC
*/
// ATL OLE DB
#include <atldbcli.h>
//#include <atldbsch.h>
#include "apDB.h"
int AtlOleDb_Test();//MFC 클래스로 지원 <- 최종선택
int ADOCpp_Test();//Import 필요
int DaoDirect_Test();//지원 끊김
int OdbcDirect_Test();//오래된 방법
int OdbcMfc_Test();//Warnning
// ATL OLEDB, ADO
// ATL(Active Template Library)
//ADO(ActiveX Data Objects)
int apDB::a1_Test()
{
AtlOleDb_Test();//선택
//ADOCpp_Test();//import 필요
//OdbcMfc_Test();//warnning
//DaoDirect_Test();//지원 제한됨
//OdbcDirect_Test();//후진 방법
return 1;
}
apDB::apDB(void)
{
// Initialize the Component Object Module Library (COM)
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
//cout<<DAM<<": Failed to CoInitialize() COM."<<endl;
_DbgStr(_T(" Failed to CoInitialize() COM.") );
//return hr;
}
}
apDB::~apDB(void)
{
}
//=======================================================================
//3000
//ADO
//[dxx-5992] 추가=2953.000000 (2.953000)
//[dxx-5992] 검색=2672.000000 (2.672000)
//[dxx-5992] 삭제=3219.000000 (3.219000)
//ATL Oledb
//[dxx-3484] 추가=2625.000000 (2.625000)
//[dxx-3484] 검색=2703.000000 (2.703000)
//[dxx-3484] 삭제=2907.000000 (2.907000)
int apDB::AtlOleDb_Test()//int argc, _TCHAR* argv[])
{
HRESULT hr = 0;
CString str;
int i=0, ret=0;
CString query;
// Data Access Method used in this sample
const TCHAR* DAM = _T("ATL OLE DB");
// Connection string for OLE DB
//LPCOLESTR lpcOleConnect = L"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\\ACESampleCode\\Northwind.accdb;Jet OLEDB:Database
Password=1L0v3Acce55;";
LPCOLESTR lpcOleConnect = L"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\test.mdb;Jet OLEDB:Database Password=;";
// To initialize the connection to a database using an OLE DB provider,
// two ATL classes are needed: CDataSource and CSession;
CDataSource dbDataSource;
CSession dbSession;
// Uses ATL's string conversion macros to convert between character encodings
USES_CONVERSION;
// Excecute the query and create a record set
CCommand<CDynamicStringAccessor> cmd;
DBORDINAL colCount = 0;
// Open the connection and initialize the data source specified by the passed
// initialization string.
hr = dbDataSource.OpenFromInitializationString(lpcOleConnect);
if (FAILED(hr))
{
//cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<endl;
_DbgStr(_T("[%s] : Unable to connect to data source %s"), DAM, OLE2CT(lpcOleConnect) );//OLE2T
}
else
{
hr = dbSession.Open(dbDataSource);
if (FAILED(hr))
{
//cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<endl;
_DbgStr(_T("[%s]: Couldn't create session on data source %s"), DAM, OLE2CT(lpcOleConnect) );//
}
else
{
CComVariant var;
hr = dbDataSource.GetProperty(DBPROPSET_DATASOURCEINFO, DBPROP_DATASOURCENAME, &var);
if (FAILED(hr) || (var.vt == VT_EMPTY))
{
//cout<<DAM<<": No Data Source Name Specified."<<endl;
_DbgStr(_T("[%s]: No Data Source Name Specified. "), DAM );
}
else
{
//cout<<DAM<<": Successfully connected to
database. Data source name:\n "
<<COLE2T(var.bstrVal)<<endl;
_DbgStr(_T("[%s]: : Successfully connected to database.
Data source name: %s"), DAM, COLE2T(var.bstrVal) );
int SIZE = 1;
//======================================================================================
//추가
_DbgStr(_T("[%s]: 추가..."), DAM);
_SPEED1(i1,1)
for(int i=0; i< SIZE; i++){
str.Format(_T("이름-%d"),i+1);
query.Format(_T("INSERT INTO tbl_test(name, num) VALUES('%s',%d);"), str, (i+1)*1000);
//_DbgStr(_T("[%s]: : SQL query: %s"), DAM, query );
hr = cmd.Open(dbSession, query);
if( SUCCEEDED(hr) ){
//_DbgStr(_T("[%s]: Insert SUCCESS"), DAM);
cmd.Close();
}else{
_DbgStr(_T("[%s]: Insert 실패"), DAM);
}
}
_SPEED2(i1,_T("추가"))
//======================================================================================
// 검색
_SPEED1(i2,1)
_DbgStr(_T("[%s]: 검색..."), DAM);
str.Format(_T("이름-%d"),i+1);
//query = _T("SELECT * FROM tbl_test WHERE num=%d;", (i+1)*1000) ;
for(int i=0; i< SIZE; i++){
str.Format(_T("이름-%d"),i+1);
query.Format( _T("SELECT * FROM tbl_test WHERE num=%d;", (i+1)*1000) );
//_DbgStr(_T("[%s]: : SQL query: %s"), DAM, query );
hr = cmd.Open(dbSession, query);
if( SUCCEEDED(hr) ){
//_DbgStr(_T("[%s]: Insert SUCCESS"), DAM);
cmd.Close();
}else{
_DbgStr(_T("[%s]: 검색 실패 - %d"), DAM, i);
}
}
_SPEED2(i2,_T("검색"))
//_DbgStr(_T("[%s]: : SQL query: %s"), DAM, query);//COLE2T(query) );
// Excecute the query and create a record set
//CCommand<CDynamicStringAccessor> cmd;
hr = cmd.Open(dbSession, query);
colCount = cmd.GetColumnCount();
if (SUCCEEDED(hr) && 0 < colCount)
{
//cout<<DAM<<": Retrieve schema info for the given result set: "<<endl;
_DbgStr(_T("[%s]: Retrieve schema info for the given result set:"), DAM );
DBORDINAL cColumns;
DBCOLUMNINFO* rgInfo = NULL;
OLECHAR* pStringsBuffer = NULL;
cmd.GetColumnInfo(&cColumns, &rgInfo, &pStringsBuffer);
for (int column = 0; column < (int)colCount; column++)
{
//cout<<" | "<<OLE2T(rgInfo[column].pwszName);
//_DbgStr(_T("[%s]: | %s"), DAM, COLE2T(rgInfo[column].pwszName) );
}
//cout<<endl;
//cout<<DAM<<": Fetch the actual data: "<<endl;
_DbgStr(_T("[%s]: Fetch the actual data: "), DAM );
int rowCount = 0;
CRowset<CDynamicStringAccessor>* pRS = (CRowset<CDynamicStringAccessor>*)&cmd;
// Loop through the rows in the result set
while (pRS->MoveNext() == S_OK)
{
for (int column = 1; column <= (int)colCount; column++)
{
TCHAR* szValue = cmd.GetString(column);
//cout<<" | "<<szValue;
//_DbgStr(_T("[%s]: | %s"), DAM, szValue );
}
//cout<<endl;
rowCount++;
}
//cout<<DAM<<": Total Row Count: "<<rowCount<<endl;
_DbgStr(_T("[%s]: Total Row Count: %d"), DAM,rowCount );
//pRS->Close();
} else {
//cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
_DbgStr(_T("[%s]: Error: Number of fields in the result set is 0. "), DAM );
}
cmd.Close();
//======================================================================================
// 삭제
_SPEED1(i3,1)
_DbgStr(_T("[%s]: 삭제..."), DAM);
for(int i=0; i< SIZE; i++){
str.Format(_T("이름-%d"),i+1);
query.Format(_T("DELETE FROM tbl_test WHERE num=%d;"), (i+1)*1000);
//_DbgStr(_T("[%s]: : SQL query: %s"), DAM, query );
hr = cmd.Open(dbSession, query);
if( SUCCEEDED(hr) ){
//_DbgStr(_T("[%s]: Insert SUCCESS"), DAM);
cmd.Close();
}else{
_DbgStr(_T("[%s]: DELETE 실패"), DAM);
}
}
_SPEED2(i3,_T("삭제"))
//======================================================================================
}
}
}
dbDataSource.Close();
dbSession.Close();
//cout<<DAM<<": Cleanup. Done."<<endl;
_DbgStr(_T("[%s]: Cleanup. Done. "), DAM );
// Release the Component Object Module Library (COM)
//CoUninitialize();
return hr;
}
/*
int apDB::ADOCpp_Test()
{
CString str, str2;
int i=0, ret=0;
//CString query;
_bstr_t query;
HRESULT hr = 0;
// Data Access Method used in this sample
const TCHAR* DAM = _T("ADO");
// Connection string for ADO
//_bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\\ACESampleCode\\Northwind.accdb;Jet OLEDB:Database
Password=1L0v3Acce55;";
_bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\test.mdb;Jet OLEDB:Database Password=;";
int argc=2;
//_TCHAR* argv[];
// COM errors are handled by C++ try/catch block
try
{
ADODB::_ConnectionPtr pConn("ADODB.Connection");
hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified);
if (SUCCEEDED(hr))
{
//cout<<DAM<<": Successfully connected to
database. Data source name:\n "
<<pConn->GetConnectionString()<<endl;
_DbgStr(_T("[%s]: Successfully connected to database. Data
source name: %s"), DAM , (LPTSTR)pConn->GetConnectionString() );
int SIZE = 3000;
//======================================================================================
//추가
_DbgStr(_T("[%s]: 추가..."), DAM);
_SPEED1(i1,1)
for(int i=0; i< SIZE; i++){
str.Format(_T("이름-%d"),i+1);
str2.Format(_T("INSERT INTO tbl_test(name, num) VALUES('%s',%d);"), str, (i+1)*1000);
//_DbgStr(_T("[%s]: : SQL query: %s"), DAM, query );
query = str2;
hr = pConn->Execute(query,NULL, ADODB::adExecuteNoRecords);
if( SUCCEEDED(hr) ){
//_DbgStr(_T("[%s]: Insert SUCCESS"), DAM);
//cmd.Close();
}else{
_DbgStr(_T("[%s]: Insert 실패"), DAM);
}
}
_SPEED2(i1,_T("추가"))
//======================================================================================
// 검색
_SPEED1(i2,1)
_DbgStr(_T("[%s]: 검색..."), DAM);
str.Format(_T("이름-%d"),i+1);
//query = _T("SELECT * FROM tbl_test WHERE num=%d;", (i+1)*1000) ;
for(int i=0; i< SIZE; i++){
str.Format(_T("이름-%d"),i+1);
str2.Format( _T("SELECT * FROM tbl_test WHERE num=%d;", (i+1)*1000) );
//_DbgStr(_T("[%s]: : SQL query: %s"), DAM, query );
query = str2;
hr = pConn->Execute(query,NULL, ADODB::adExecuteNoRecords);
if( SUCCEEDED(hr) ){
//_DbgStr(_T("[%s]: Insert SUCCESS"), DAM);
//cmd.Close();
}else{
_DbgStr(_T("[%s]: 검색 실패 - %d"), DAM, i);
}
}
_SPEED2(i2,_T("검색"))
// Prepare SQL query.
//_bstr_t query = "SELECT Customers.[Company],
Customers.[First Name] FROM Customers ORDER BY Customers.[Company]
ASC;";
//_bstr_t query1 = _T("SELECT * FROM tbl_test;");
str = _T("SELECT * FROM tbl_test;"); query = str;
//cout<<DAM<<": SQL query:\n "<<query<<endl;
_DbgStr(_T("[%s] : SQL query: %s "), DAM, (LPTSTR)query );
// Execute the query and create a record set
ADODB::_RecordsetPtr pRS("ADODB.Recordset");
hr = pRS->Open( query, _variant_t((IDispatch *) pConn,
true), ADODB::adOpenUnspecified, ADODB::adLockUnspecified,
ADODB::adCmdText);
if (SUCCEEDED(hr))
{
//cout<<DAM<<": Retrieve schema info for the given result set: "<<endl;
_DbgStr(_T("[%s] : Retrieve schema info for the given result set: "), DAM );
ADODB::Fields* pFields = NULL;
hr = pRS->get_Fields(&pFields);
if (SUCCEEDED(hr) && pFields && pFields->GetCount() > 0)
{
for (long column = 0; column < pFields->GetCount(); column++)
{
//cout<<" | "<<_bstr_t(pFields->GetItem(column)->GetName());
//_DbgStr(_T("[%s]| %s"), DAM , (LPTSTR)_bstr_t(pFields->GetItem(column)->GetName()) );
}
//cout<<endl;
}
else
{
//cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
_DbgStr(_T("[%s]: Error: Number of fields in the result set is 0."), DAM );
}
//cout<<DAM<<": Fetch the actual data: "<<endl;
_DbgStr(_T("[%s]: Fetch the actual data:"), DAM );
int rowCount = 0;
while (!pRS->AdoNSEOF)
{
for (long column = 0; column < pFields->GetCount(); column++)
{
//cout<<" | "<<_bstr_t(pFields->GetItem(column)->GetValue());
//_DbgStr(_T("[%s]| %s"), DAM , (LPTSTR)_bstr_t(pFields->GetItem(column)->GetValue()) );
}
//cout<<endl;
pRS->MoveNext();
rowCount++;
}
//cout<<DAM<<": Total Row Count: "<<rowCount<<endl;
_DbgStr(_T("[%s]: Total Row Count: %d"), DAM , rowCount );
}
pRS->Close();
//======================================================================================
// 삭제
_SPEED1(i3,1)
_DbgStr(_T("[%s]: 삭제..."), DAM);
for(int i=0; i< SIZE; i++){
str.Format(_T("이름-%d"),i+1);
str2.Format(_T("DELETE FROM tbl_test WHERE num=%d;"), (i+1)*1000);
//_DbgStr(_T("[%s]: : SQL query: %s"), DAM, query );
query = str2;
hr = pConn->Execute(query,NULL, ADODB::adExecuteNoRecords);
if( SUCCEEDED(hr) ){
//_DbgStr(_T("[%s]: Insert SUCCESS"), DAM);
//cmd.Close();
}else{
_DbgStr(_T("[%s]: DELETE 실패"), DAM);
}
}
_SPEED2(i3,_T("삭제"))
//======================================================================================
pConn->Close();
//cout<<DAM<<": Cleanup. Done."<<endl;
_DbgStr(_T("[%s] : Cleanup. Done. "), DAM );
}
else
{
//cout<<DAM<<": Unable to connect to data source: "<<bstrConnect<<endl;
_DbgStr(_T("[%s]: Unable to connect to data source: %s"), DAM , (LPTSTR)bstrConnect );
}
}
catch(_com_error& e)
{
//cout<<DAM<<": _com_error: "<<e.Description()<<endl;
_DbgStr(_T("[%s]: _com_error: %s"), DAM , (LPTSTR)e.Description() );
}
// Release the Component Object Module Library (COM)
//CoUninitialize();
//int ch; cin >>ch;
return hr;
}
//================================================================
//#import "C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\OFFICE14\\ACEDAO.dll" rename( "EOF", "AdoNSEOF" )
int DaoDirect_Test()
{
// Data Access Method used in this sample
const TCHAR* DAM = _T("Direct DAO");
// Connection string for Direct DAO
_bstr_t bstrConnect = _T("d:\\test.mdb");//"C:\\ACESampleCode\\Northwind.accdb";
//int argc, _TCHAR* argv[]
// Initialize the Component Object Module Library (COM)
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
cout<<DAM<<": Failed to CoInitialize() COM."<<endl;
return hr;
}
// Create an instance of the engine
DAO::_DBEngine* pEngine = NULL;
// The CoCreateInstance helper function provides a convenient shortcut by connecting
// to the class object associated with the specified CLSID, creating an
// uninitialized instance, and releasing the class object.
hr = CoCreateInstance(
__uuidof(DAO::DBEngine),
NULL,
CLSCTX_ALL,
IID_IDispatch,
(LPVOID*)&pEngine);
if (SUCCEEDED(hr) && pEngine)
{
// COM errors are handled by C++ try/catch block
try
{
DAO::DatabasePtr pDbPtr = NULL;
pDbPtr = pEngine->OpenDatabase(bstrConnect, false, false, ";PWD=" );//, ";PWD=1L0v3Acce55;");
if (pDbPtr)
{
//cout<<DAM<<": Successfully connected to
database. Data source name:\n "
<<pDbPtr->GetName()<<endl;
_DbgStr(_T(": Successfully connected to database. Data source name: %s"), (LPTSTR)pDbPtr->GetName() );
// Prepare SQL query.
//_bstr_t query = "SELECT Customers.[Company],
Customers.[First Name] FROM Customers ORDER BY Customers.[Company]
ASC;";
_bstr_t query = "SELECT * FROM tbl_test;";
//cout<<DAM<<": SQL query:\n "<<query<<endl;
_DbgStr(_T(": SQL query: %s"), (LPTSTR)query );
// Execute the query and create a record set
DAO::RecordsetPtr pRS = NULL;
pRS = pDbPtr->OpenRecordset(query, _variant_t(DAO::dbOpenDynaset));
if (pRS && 0 < pRS->RecordCount)
{
//cout<<DAM<<": Retrieve schema info for the given result set: "<<endl;
_DbgStr(_T(": Retrieve schema info for the given result set:") );
DAO::FieldsPtr pFields = NULL;
pFields = pRS->GetFields();
if (pFields && pFields->Count > 0)
{
//칼럼이름
for (short column = 0; column < pFields->Count; column++)
{
//cout<<" | "<<pFields->GetItem(column)->GetName();
_DbgStr(_T("|- %s"), (LPTSTR)pFields->GetItem(column)->GetName() );
}
//cout<<endl;
}
else
{
//cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
_DbgStr(_T(": Error: Number of fields in the result set is 0."));
}
//cout<<DAM<<": Fetch the actual data: "<<endl;
_DbgStr(_T(": Fetch the actual data: "));
// Loop through the rows in the result set
while (!pRS->EndOfFile)
{
for (short column = 0; column < pFields->Count; column++)
{
//cout<<" | "<<_bstr_t(pFields->GetItem(column)->GetValue());
_DbgStr(_T("|- %s"), (LPTSTR)_bstr_t(pFields->GetItem(column)->GetValue()) );
}
//cout<<endl;
pRS->MoveNext();
}
//cout<<DAM<<": Total Row Count: "<<pRS->RecordCount<<endl;
_DbgStr(_T(": Total Row Count: %d"), pRS->RecordCount );
}
// Close record set and database
pRS->Close();
pDbPtr->Close();
pDbPtr = NULL;
}
else
{
//cout<<DAM<<": Unable to connect to data source: "<<bstrConnect<<endl;
_DbgStr(_T(": Unable to connect to data source: %s"), (LPTSTR)bstrConnect );
}
}
catch(_com_error& e)
{
//cout<<DAM<<": _com_error: "<<e.ErrorMessage()<<endl;
_DbgStr(_T(": _com_error: "), (LPTSTR)e.ErrorMessage() );
}
pEngine->Release();
pEngine = NULL;
//cout<<DAM<<": Cleanup. Done."<<endl;
_DbgStr(_T(": Cleanup. Done."));
}
else
{
//cout<<DAM<<": Cannot instantiate DBEngine object."<<endl;
_DbgStr(_T(": Cannot instantiate DBEngine object.") );
}
// Release the Component Object Module Library (COM)
CoUninitialize();
return hr;
}
//=======================================================
//Data Programming with Access 2010
//http://msdn.microsoft.com/en-us/library/3b86dde9-ebd6-4a3e-a824-3f50c5574914.aspx
int OdbcDirect_Test()
{
// Data Access Method used in this sample
const TCHAR* DAM = _T("Direct ODBC");
// Connection string for Direct ODBC
//SQLCHAR szDSN[256] = "Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DSN='';DBQ=C:\\ACESampleCode\\Northwind.accdb;PWD=1L0v3Acce55;";
SQLTCHAR szDSN[256] = _T("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=d:\\test.mdb;PWD=;");
HENV hEnv;
HDBC hDbc;
// ODBC API return status
SQLRETURN rc;
SQLSMALLINT iConnStrLength2Ptr;
SQLTCHAR szConnStrOut[255];
//SQLCHAR* query = (SQLCHAR*)"SELECT Customers.[Company],
Customers.[First Name] FROM Customers ORDER BY Customers.[Company]
ASC;";
SQLTCHAR* query = (SQLTCHAR*)_T("SELECT * FROM tbl_test;");
SQLTCHAR chval1[128], chval2[128], colName[128];
SQLINTEGER ret1, ret2;
int nVal=0, t=0, ret=0;
// Number of rows and columns in result set
SQLINTEGER rowCount = 0;
SQLSMALLINT fieldCount = 0, column = 0;
HSTMT hStmt;
// Allocate an environment handle
rc = SQLAllocEnv(&hEnv);
// Allocate a connection handle
rc = SQLAllocConnect(hEnv, &hDbc);
// Connect to the 'Northwind 2007.accdb' database
rc = SQLDriverConnect(hDbc, NULL, szDSN, _countof(szDSN),
szConnStrOut, 255, &iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc))
{
_DbgStr(_T("%s: Successfully connected to database. Data source name: %s"), DAM, szConnStrOut);
// Prepare SQL query
_DbgStr(_T("%s: SQL query: %s"), DAM, query);
rc = SQLAllocStmt(hDbc,&hStmt);
rc = SQLPrepare(hStmt, query, SQL_NTS);
// Bind result set columns to the local buffers
t=sizeof(chval2);
//rc = SQLBindCol(hStmt, 1, SQL_C_LONG, &nVal, sizeof(nVal), &ret1);//SQL_C_CHAR
rc = SQLBindCol(hStmt, 1, SQL_C_TCHAR, chval1, sizeof(chval1) , &ret1);
rc = SQLBindCol(hStmt, 2, SQL_C_TCHAR, chval2, sizeof(chval2) , &ret2);
// Execute the query and create a record set
rc = SQLExecute(hStmt);
if (SQL_SUCCEEDED(rc))
{
_DbgStr(_T("%s: Retrieve schema info for the given result set:"), DAM);
SQLNumResultCols(hStmt, &fieldCount);
if (fieldCount > 0)
{
for (column = 1; column <= fieldCount; column++)
{
SQLDescribeCol(hStmt, column, colName, sizeof(colName), 0, 0, 0, 0, 0);
_DbgStr(_T(" | %s"), colName);
}
//printf("\n");
}
else
{
_DbgStr(_T("%s: Error: Number of fields in the result set is 0."), DAM);
}
_DbgStr(_T("%s: Fetch the actual data:"), DAM);
// Loop through the rows in the result set
chval1[0]=0;chval2[0]=0;nVal=0;
rc = SQLFetch(hStmt);
while (SQL_SUCCEEDED(rc))
{
_DbgStr(_T(" | %s | %s"), chval1, chval2);
//_DbgStr(_T(" | %d | %s"), nVal, chval2);
rc = SQLFetch(hStmt);
rowCount++;
};
_DbgStr(_T("%s: Total Row Count: %d"), DAM, rowCount);
rc = SQLFreeStmt(hStmt, SQL_DROP);
}
}
else
{
_DbgStr(_T("%s: Couldn't connect to %s."), DAM, szDSN);
}
// Disconnect and free up allocated handles
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
_DbgStr(_T("%s: Cleanup. Done."), DAM);
return 1;
}
//=======================================================================
int OdbcMfc_Test()//(int argc, _TCHAR* argv[])
{
// Data Access Method used in this sample
const TCHAR* DAM = _T("MFC ODBC");
// Connection string for MFC ODBC
//LPCTSTR lpszConnect = _T("Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DSN='';DBQ=C:\\ACESampleCode\\Northwind.accdb;PWD=1L0v3Acce55;");
//LPCTSTR lpszConnect = _T("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=d:\\test.mdb;PWD=;");
LPCTSTR lpszConnect = _T("DBQ=d:\\test.mdb;Driver={Microsoft Access
Driver (*.mdb, *.accdb)};");//DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;");//DefaultDir=;ReadOnly=0;
CString str;
BOOL result = TRUE;
CDatabase db;
TRY
{
//db.Open(NULL,false,false,lpszConnect);
result = db.OpenEx(lpszConnect, CDatabase::noOdbcDialog);//CDatabase::openReadOnly |
str = db.GetConnect();
_DbgStr(_T("[%s]: %s "), DAM, str);
return 0;
if (FALSE == result)
{
//cout<<DAM<<": Unable to connect to data source "<<lpszConnect<<endl;
_DbgStr(_T("[%s]: Unable to connect to data source %s "), DAM, lpszConnect);
return result;
}
//cout<<DAM<<": Successfully connected to database.
Data source name:\n " <<db.GetDatabaseName()<<endl;
_DbgStr(_T("\n[%s]: Successfully connected to database. Data source name: %s "), DAM, db.GetDatabaseName());
// Prepare SQL query
//LPCTSTR query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers ORDER BY Customers.[Company] ASC;";
LPCTSTR query = _T("SELECT * FROM tbl_test;");
//cout<<DAM<<": SQL query:\n "<<query<<endl;
_DbgStr(_T("[%s]: SQL query: %s "), DAM, query );
// Execute the query and create a record set
CRecordset rs(&db);
result = rs.Open(CRecordset::dynaset, query, CRecordset::none);
if (result == TRUE)
{
//cout<<DAM<<": Retrieve schema info for the given result set: "<<endl;
_DbgStr(_T("[%s]: Retrieve schema info for the given result set: "), DAM );
CODBCFieldInfo fInfo;
short sFieldCount = rs.GetODBCFieldCount();
if (sFieldCount > 0)
{
for (short column = 0; column < sFieldCount; column++)
{
CODBCFieldInfo fInfo;
rs.GetODBCFieldInfo(column, fInfo);
//cout<<" | "<<fInfo.m_strName;
_DbgStr(_T("[%s]: | %s "), DAM, fInfo.m_strName );
}
//cout<<endl;
}
else
{
//cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
_DbgStr(_T("[%s]: Error: Number of fields in the result set is 0. "), DAM );
}
//cout<<DAM<<": Fetch the actual data: "<<endl;
_DbgStr(_T("[%s]: Fetch the actual data: "), DAM );
CDBVariant var;
CString value;
// Loop through the rows in the result set
int rowCount = 0;
while (!rs.IsEOF())
{
for (short column = 0; column < sFieldCount; column++)
{
rs.GetFieldValue(column, var);
switch (var.m_dwType)
{
case DBVT_STRING:
value.Format(_T("%s"), var.m_pstring->GetBuffer(var.m_pstring->GetLength()));
break;
case DBVT_ASTRING:
value.Format(_T("%s"), var.m_pstringA->GetBuffer(var.m_pstringA->GetLength()));
break;
case DBVT_WSTRING:
value.Format(_T("%s"), var.m_pstringW->GetBuffer(var.m_pstringW->GetLength()));
break;
case DBVT_SHORT: case DBVT_LONG: case DBVT_SINGLE: case DBVT_DOUBLE:
value.Format(_T("%d"), var.m_iVal );
break;
default:
value = _T("");
}
//cout<<" | "<<value;
_DbgStr(_T("[%s] | %s"), DAM , value);
}
//cout<<endl;
rowCount++;
rs.MoveNext();
}
//cout<<DAM<<": Total Row Count: "<<rowCount<<endl;
_DbgStr(_T("[%s] : Total Row Count: %d"), DAM , rowCount);
}
}
CATCH_ALL(e)
{
TCHAR errMsg[255];
e->GetErrorMessage(errMsg, 255);
// cout<<DAM<<": CException: "<<errMsg<<endl;
_DbgStr(_T("[%s] : CException: %s"), DAM , errMsg);
}
END_CATCH_ALL
db.Close();
//cout<<DAM<<": Cleanup. Done."<<endl;
_DbgStr(_T("[%s] : Cleanup. Done. "), DAM );
return result;
}
*/
//===============================================================
'Code' 카테고리의 다른 글
OllyDbg 단축키 (0) | 2012.08.19 |
---|---|
OllyDbg Command (0) | 2012.08.19 |
APM 설치 (0) | 2012.08.11 |
Memory Mapped File (0) | 2012.08.01 |
멀티 쓰레드 동기화(Syncronazation) (0) | 2012.08.01 |