//
//
MODULE: Ado2.h
//
//
AUTHOR: Carlos Antollini
//
//
mailto: cantollini@hotmail.com
//
//
Date: 07/02/2003
//
//
Version 2.10
//
#if
!defined(AFX_ADO2_H_INCLUDED_)
#define
AFX_ADO2_H_INCLUDED_
#if
_MSC_VER >= 1000
#pragma once
#endif
//
_MSC_VER >= 1000
#include
<
afx.h
>
#include
<
afxdisp.h
>
#include
<
math.h
>
//
#include "C:\Program Files\Microsoft Analysis Services\Samples\Include\msmd.h"
#pragma warning (disable:
4146
)
//
CG : In order to use this code against a different version of ADO, the appropriate
//
ADO library needs to be used in the #import statement
#import
"
C:\Program Files\Common Files\System\ADO\msado15.dll
"
rename(
"
EOF
"
,
"
EndOfFile
"
)
#import
"
C:\Program Files\Common Files\System\ado\MSJRO.DLL
"
no_namespace
using
namespace
ADODB;
#pragma warning (
default
:
4146
)
#include
"
icrsint.h
"
class
CADOCommand;
struct
CADOFieldInfo
{
char
m_strName[
30
];
short
m_nType;
long
m_lSize;
long
m_lDefinedSize;
long
m_lAttributes;
short
m_nOrdinalPosition;
BOOL m_bRequired;
BOOL m_bAllowZeroLength;
long
m_lCollatingOrder;
};
CString IntToStr(
int
nVal);
CString LongToStr(
long
lVal);
CString ULongToStr(unsigned
long
ulVal);
CString DblToStr(
double
dblVal,
int
ndigits
=
20
);
CString DblToStr(
float
fltVal);
class
CJetEngine
{
public
:
CJetEngine()
{
;
}
virtual
~
CJetEngine()
{
;
}
static
BOOL CCompactDatabase(CString strDatabaseSource, CString strDatabaseDestination);
};
class
CADODatabase
{
public
:
enum
cadoConnectModeEnum
{
connectModeUnknown
=
adModeUnknown,
connectModeRead
=
adModeRead,
connectModeWrite
=
adModeWrite,
connectModeReadWrite
=
adModeReadWrite,
connectModeShareDenyRead
=
adModeShareDenyRead,
connectModeShareDenyWrite
=
adModeShareDenyWrite,
connectModeShareExclusive
=
adModeShareExclusive,
connectModeShareDenyNone
=
adModeShareDenyNone
};
CADODatabase()
{
::CoInitialize(NULL);
m_pConnection
=
NULL;
m_strConnection
=
_T(
""
);
m_strLastError
=
_T(
""
);
m_dwLastError
=
0
;
m_pConnection.CreateInstance(__uuidof(Connection));
m_nRecordsAffected
=
0
;
m_nConnectionTimeout
=
0
;
}
virtual
~
CADODatabase()
{
Close();
m_pConnection.Release();
m_pConnection
=
NULL;
m_strConnection
=
_T(
""
);
m_strLastError
=
_T(
""
);
m_dwLastError
=
0
;
::CoUninitialize();
}
BOOL Open(LPCTSTR lpstrConnection
=
_T(
""
), LPCTSTR lpstrUserID
=
_T(
""
), LPCTSTR lpstrPassword
=
_T(
""
));
_ConnectionPtr GetActiveConnection()
{
return
m_pConnection;};
BOOL Execute(LPCTSTR lpstrExec);
int
GetRecordsAffected()
{
return
m_nRecordsAffected;};
DWORD GetRecordCount(_RecordsetPtr m_pRs);
long
BeginTransaction()
{
return
m_pConnection
->
BeginTrans();};
long
CommitTransaction()
{
return
m_pConnection
->
CommitTrans();};
long
RollbackTransaction()
{
return
m_pConnection
->
RollbackTrans();};
BOOL IsOpen();
void
Close();
void
SetConnectionMode(cadoConnectModeEnum nMode)
{m_pConnection
->
PutMode((
enum
ConnectModeEnum)nMode);};
void
SetConnectionString(LPCTSTR lpstrConnection)
{m_strConnection
=
lpstrConnection;};
CString GetConnectionString()
{
return
m_strConnection;};
CString GetLastErrorString()
{
return
m_strLastError;};
DWORD GetLastError()
{
return
m_dwLastError;};
CString GetErrorDescription()
{
return
m_strErrorDescription;};
void
SetConnectionTimeout(
long
nConnectionTimeout
=
30
)
{m_nConnectionTimeout
=
nConnectionTimeout;};
protected
:
void
dump_com_error(_com_error
&
e);
public
:
_ConnectionPtr m_pConnection;
protected
:
CString m_strConnection;
CString m_strLastError;
CString m_strErrorDescription;
DWORD m_dwLastError;
int
m_nRecordsAffected;
long
m_nConnectionTimeout;
};
class
CADORecordset
{
public
:
BOOL Clone(CADORecordset
&
pRs);
enum
cadoOpenEnum
{
openUnknown
=
0
,
openQuery
=
1
,
openTable
=
2
,
openStoredProc
=
3
};
enum
cadoEditEnum
{
dbEditNone
=
0
,
dbEditNew
=
1
,
dbEdit
=
2
};
enum
cadoPositionEnum
{
positionUnknown
=
-
1
,
positionBOF
=
-
2
,
positionEOF
=
-
3
};
enum
cadoSearchEnum
{
searchForward
=
1
,
searchBackward
=
-
1
};
enum
cadoDataType
{
typeEmpty
=
ADODB::adEmpty,
typeTinyInt
=
ADODB::adTinyInt,
typeSmallInt
=
ADODB::adSmallInt,
typeInteger
=
ADODB::adInteger,
typeBigInt
=
ADODB::adBigInt,
typeUnsignedTinyInt
=
ADODB::adUnsignedTinyInt,
typeUnsignedSmallInt
=
ADODB::adUnsignedSmallInt,
typeUnsignedInt
=
ADODB::adUnsignedInt,
typeUnsignedBigInt
=
ADODB::adUnsignedBigInt,
typeSingle
=
ADODB::adSingle,
typeDouble
=
ADODB::adDouble,
typeCurrency
=
ADODB::adCurrency,
typeDecimal
=
ADODB::adDecimal,
typeNumeric
=
ADODB::adNumeric,
typeBoolean
=
ADODB::adBoolean,
typeError
=
ADODB::adError,
typeUserDefined
=
ADODB::adUserDefined,
typeVariant
=
ADODB::adVariant,
typeIDispatch
=
ADODB::adIDispatch,
typeIUnknown
=
ADODB::adIUnknown,
typeGUID
=
ADODB::adGUID,
typeDate
=
ADODB::adDate,
typeDBDate
=
ADODB::adDBDate,
typeDBTime
=
ADODB::adDBTime,
typeDBTimeStamp
=
ADODB::adDBTimeStamp,
typeBSTR
=
ADODB::adBSTR,
typeChar
=
ADODB::adChar,
typeVarChar
=
ADODB::adVarChar,
typeLongVarChar
=
ADODB::adLongVarChar,
typeWChar
=
ADODB::adWChar,
typeVarWChar
=
ADODB::adVarWChar,
typeLongVarWChar
=
ADODB::adLongVarWChar,
typeBinary
=
ADODB::adBinary,
typeVarBinary
=
ADODB::adVarBinary,
typeLongVarBinary
=
ADODB::adLongVarBinary,
typeChapter
=
ADODB::adChapter,
typeFileTime
=
ADODB::adFileTime,
typePropVariant
=
ADODB::adPropVariant,
typeVarNumeric
=
ADODB::adVarNumeric,
typeArray
=
ADODB::adVariant
};
enum
cadoSchemaType
{
schemaSpecific
=
adSchemaProviderSpecific,
schemaAsserts
=
adSchemaAsserts,
schemaCatalog
=
adSchemaCatalogs,
schemaCharacterSet
=
adSchemaCharacterSets,
schemaCollections
=
adSchemaCollations,
schemaColumns
=
adSchemaColumns,
schemaConstraints
=
adSchemaCheckConstraints,
schemaConstraintColumnUsage
=
adSchemaConstraintColumnUsage,
schemaConstraintTableUsage
=
adSchemaConstraintTableUsage,
shemaKeyColumnUsage
=
adSchemaKeyColumnUsage,
schemaTableConstraints
=
adSchemaTableConstraints,
schemaColumnsDomainUsage
=
adSchemaColumnsDomainUsage,
schemaIndexes
=
adSchemaIndexes,
schemaColumnPrivileges
=
adSchemaColumnPrivileges,
schemaTablePrivileges
=
adSchemaTablePrivileges,
schemaUsagePrivileges
=
adSchemaUsagePrivileges,
schemaProcedures
=
adSchemaProcedures,
schemaTables
=
adSchemaTables,
schemaProviderTypes
=
adSchemaProviderTypes,
schemaViews
=
adSchemaViews,
schemaProcedureParameters
=
adSchemaProcedureParameters,
schemaForeignKeys
=
adSchemaForeignKeys,
schemaPrimaryKeys
=
adSchemaPrimaryKeys,
schemaProcedureColumns
=
adSchemaProcedureColumns,
schemaDBInfoKeywords
=
adSchemaDBInfoKeywords,
schemaDBInfoLiterals
=
adSchemaDBInfoLiterals,
schemaCubes
=
adSchemaCubes,
schemaDimensions
=
adSchemaDimensions,
schemaHierarchies
=
adSchemaHierarchies,
schemaLevels
=
adSchemaLevels,
schemaMeasures
=
adSchemaMeasures,
schemaProperties
=
adSchemaProperties,
schemaMembers
=
adSchemaMembers,
};
BOOL SetFieldValue(
int
nIndex,
int
nValue);
BOOL SetFieldValue(LPCTSTR lpFieldName,
int
nValue);
BOOL SetFieldValue(
int
nIndex,
long
lValue);
BOOL SetFieldValue(LPCTSTR lpFieldName,
long
lValue);
BOOL SetFieldValue(
int
nIndex, unsigned
long
lValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, unsigned
long
lValue);
BOOL SetFieldValue(
int
nIndex,
double
dblValue);
BOOL SetFieldValue(LPCTSTR lpFieldName,
double
dblValue);
BOOL SetFieldValue(
int
nIndex, CString strValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, CString strValue);
BOOL SetFieldValue(
int
nIndex, COleDateTime time);
BOOL SetFieldValue(LPCTSTR lpFieldName, COleDateTime time);
BOOL SetFieldValue(
int
nIndex,
bool
bValue);
BOOL SetFieldValue(LPCTSTR lpFieldName,
bool
bValue);
BOOL SetFieldValue(
int
nIndex, COleCurrency cyValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, COleCurrency cyValue);
BOOL SetFieldValue(
int
nIndex, _variant_t vtValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, _variant_t vtValue);
BOOL SetFieldEmpty(
int
nIndex);
BOOL SetFieldEmpty(LPCTSTR lpFieldName);
void
CancelUpdate();
BOOL Update();
void
Edit();
BOOL AddNew();
BOOL AddNew(CADORecordBinding
&
pAdoRecordBinding);
BOOL Find(LPCTSTR lpFind,
int
nSearchDirection
=
CADORecordset::searchForward);
BOOL FindFirst(LPCTSTR lpFind);
BOOL FindNext();
CADORecordset();
CADORecordset(CADODatabase
*
pAdoDatabase);
virtual
~
CADORecordset()
{
Close();
if
(m_pRecordset)
m_pRecordset.Release();
if
(m_pCmd)
m_pCmd.Release();
m_pRecordset
=
NULL;
m_pCmd
=
NULL;
m_pRecBinding
=
NULL;
m_strQuery
=
_T(
""
);
m_strLastError
=
_T(
""
);
m_dwLastError
=
0
;
m_nEditStatus
=
dbEditNone;
}
CString GetQuery()
{
return
m_strQuery;};
void
SetQuery(LPCSTR strQuery)
{m_strQuery
=
strQuery;};
BOOL RecordBinding(CADORecordBinding
&
pAdoRecordBinding);
DWORD GetRecordCount();
BOOL IsOpen();
void
Close();
BOOL Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec
=
_T(
""
),
int
nOption
=
CADORecordset::openUnknown);
BOOL Open(LPCTSTR lpstrExec
=
_T(
""
),
int
nOption
=
CADORecordset::openUnknown);
BOOL OpenSchema(
int
nSchema, LPCTSTR SchemaID
=
_T(
""
));
long
GetFieldCount()
{
return
m_pRecordset
->
Fields
->
GetCount();};
BOOL GetFieldValue(LPCTSTR lpFieldName,
int
&
nValue);
BOOL GetFieldValue(
int
nIndex,
int
&
nValue);
BOOL GetFieldValue(LPCTSTR lpFieldName,
long
&
lValue);
BOOL GetFieldValue(
int
nIndex,
long
&
lValue);
BOOL GetFieldValue(LPCTSTR lpFieldName, unsigned
long
&
ulValue);
BOOL GetFieldValue(
int
nIndex, unsigned
long
&
ulValue);
BOOL GetFieldValue(LPCTSTR lpFieldName,
double
&
dbValue);
BOOL GetFieldValue(
int
nIndex,
double
&
dbValue);
BOOL GetFieldValue(LPCTSTR lpFieldName, CString
&
strValue, CString strDateFormat
=
_T(
""
));
BOOL GetFieldValue(
int
nIndex, CString
&
strValue, CString strDateFormat
=
_T(
""
));
BOOL GetFieldValue(LPCTSTR lpFieldName, COleDateTime
&
time);
BOOL GetFieldValue(
int
nIndex, COleDateTime
&
time);
BOOL GetFieldValue(
int
nIndex,
bool
&
bValue);
BOOL GetFieldValue(LPCTSTR lpFieldName,
bool
&
bValue);
BOOL GetFieldValue(
int
nIndex, COleCurrency
&
cyValue);
BOOL GetFieldValue(LPCTSTR lpFieldName, COleCurrency
&
cyValue);
BOOL GetFieldValue(
int
nIndex, _variant_t
&
vtValue);
BOOL GetFieldValue(LPCTSTR lpFieldName, _variant_t
&
vtValue);
BOOL IsFieldNull(LPCTSTR lpFieldName);
BOOL IsFieldNull(
int
nIndex);
BOOL IsFieldEmpty(LPCTSTR lpFieldName);
BOOL IsFieldEmpty(
int
nIndex);
BOOL IsEof()
{
return
m_pRecordset
->
EndOfFile
==
VARIANT_TRUE;};
BOOL IsEOF()
{
return
m_pRecordset
->
EndOfFile
==
VARIANT_TRUE;};
BOOL IsBof()
{
return
m_pRecordset
->
BOF
==
VARIANT_TRUE;};
BOOL IsBOF()
{
return
m_pRecordset
->
BOF
==
VARIANT_TRUE;};
void
MoveFirst()
{m_pRecordset
->
MoveFirst();};
void
MoveNext()
{m_pRecordset
->
MoveNext();};
void
MovePrevious()
{m_pRecordset
->
MovePrevious();};
void
MoveLast()
{m_pRecordset
->
MoveLast();};
long
GetAbsolutePage()
{
return
m_pRecordset
->
GetAbsolutePage();};
void
SetAbsolutePage(
int
nPage)
{m_pRecordset
->
PutAbsolutePage((
enum
PositionEnum)nPage);};
long
GetPageCount()
{
return
m_pRecordset
->
GetPageCount();};
long
GetPageSize()
{
return
m_pRecordset
->
GetPageSize();};
void
SetPageSize(
int
nSize)
{m_pRecordset
->
PutPageSize(nSize);};
long
GetAbsolutePosition()
{
return
m_pRecordset
->
GetAbsolutePosition();};
void
SetAbsolutePosition(
int
nPosition)
{m_pRecordset
->
PutAbsolutePosition((
enum
PositionEnum)nPosition);};
BOOL GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo
*
fldInfo);
BOOL GetFieldInfo(
int
nIndex, CADOFieldInfo
*
fldInfo);
BOOL AppendChunk(LPCTSTR lpFieldName, LPVOID lpData, UINT nBytes);
BOOL AppendChunk(
int
nIndex, LPVOID lpData, UINT nBytes);
BOOL GetChunk(LPCTSTR lpFieldName, CString
&
strValue);
BOOL GetChunk(
int
nIndex, CString
&
strValue);
BOOL GetChunk(LPCTSTR lpFieldName, LPVOID pData);
BOOL GetChunk(
int
nIndex, LPVOID pData);
CString GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull,
long
numRows
=
0
);
CString GetLastErrorString()
{
return
m_strLastError;};
DWORD GetLastError()
{
return
m_dwLastError;};
void
GetBookmark()
{m_varBookmark
=
m_pRecordset
->
Bookmark;};
BOOL SetBookmark();
BOOL Delete();
BOOL IsConnectionOpen()
{
return
m_pConnection
!=
NULL
&&
m_pConnection
->
GetState()
!=
adStateClosed;};
_RecordsetPtr GetRecordset()
{
return
m_pRecordset;};
_ConnectionPtr GetActiveConnection()
{
return
m_pConnection;};
BOOL SetFilter(LPCTSTR strFilter);
BOOL SetSort(LPCTSTR lpstrCriteria);
BOOL SaveAsXML(LPCTSTR lpstrXMLFile);
BOOL OpenXML(LPCTSTR lpstrXMLFile);
BOOL Execute(CADOCommand
*
pCommand);
BOOL Requery();
public
:
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCmd;
protected
:
_ConnectionPtr m_pConnection;
int
m_nSearchDirection;
CString m_strFind;
_variant_t m_varBookFind;
_variant_t m_varBookmark;
int
m_nEditStatus;
CString m_strLastError;
DWORD m_dwLastError;
void
dump_com_error(_com_error
&
e);
IADORecordBinding
*
m_pRecBinding;
CString m_strQuery;
protected
:
BOOL PutFieldValue(LPCTSTR lpFieldName, _variant_t vtFld);
BOOL PutFieldValue(_variant_t vtIndex, _variant_t vtFld);
BOOL GetFieldInfo(FieldPtr pField, CADOFieldInfo
*
fldInfo);
BOOL GetChunk(FieldPtr pField, CString
&
strValue);
BOOL GetChunk(FieldPtr pField, LPVOID lpData);
BOOL AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes);
};
class
CADOParameter
{
public
:
enum
cadoParameterDirection
{
paramUnknown
=
adParamUnknown,
paramInput
=
adParamInput,
paramOutput
=
adParamOutput,
paramInputOutput
=
adParamInputOutput,
paramReturnValue
=
adParamReturnValue
};
CADOParameter(
int
nType,
long
lSize
=
0
,
int
nDirection
=
paramInput, CString strName
=
_T(
""
));
virtual
~
CADOParameter()
{
m_pParameter.Release();
m_pParameter
=
NULL;
m_strName
=
_T(
""
);
}
BOOL SetValue(
int
nValue);
BOOL SetValue(
long
lValue);
BOOL SetValue(
double
dbValue);
BOOL SetValue(CString strValue);
BOOL SetValue(COleDateTime time);
BOOL SetValue(_variant_t vtValue);
BOOL GetValue(
int
&
nValue);
BOOL GetValue(
long
&
lValue);
BOOL GetValue(
double
&
dbValue);
BOOL GetValue(CString
&
strValue, CString strDateFormat
=
_T(
""
));
BOOL GetValue(COleDateTime
&
time);
BOOL GetValue(_variant_t
&
vtValue);
void
SetPrecision(
int
nPrecision)
{m_pParameter
->
PutPrecision(nPrecision);};
void
SetScale(
int
nScale)
{m_pParameter
->
PutNumericScale(nScale);};
void
SetName(CString strName)
{m_strName
=
strName;};
CString GetName()
{
return
m_strName;};
int
GetType()
{
return
m_nType;};
_ParameterPtr GetParameter()
{
return
m_pParameter;};
protected
:
void
dump_com_error(_com_error
&
e);
protected
:
_ParameterPtr m_pParameter;
CString m_strName;
int
m_nType;
CString m_strLastError;
DWORD m_dwLastError;
};
class
CADOCommand
{
public
:
enum
cadoCommandType
{
typeCmdText
=
adCmdText,
typeCmdTable
=
adCmdTable,
typeCmdTableDirect
=
adCmdTableDirect,
typeCmdStoredProc
=
adCmdStoredProc,
typeCmdUnknown
=
adCmdUnknown,
typeCmdFile
=
adCmdFile
};
CADOCommand(CADODatabase
*
pAdoDatabase, CString strCommandText
=
_T(
""
),
int
nCommandType
=
typeCmdStoredProc);
virtual
~
CADOCommand()
{
m_pCommand.Release();
m_pCommand
=
NULL;
m_strCommandText
=
_T(
""
);
}
void
SetTimeout(
long
nTimeOut)
{m_pCommand
->
PutCommandTimeout(nTimeOut);};
void
SetText(CString strCommandText);
void
SetType(
int
nCommandType);
int
GetType()
{
return
m_nCommandType;};
BOOL AddParameter(CADOParameter
*
pAdoParameter);
BOOL AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize,
int
nValue);
BOOL AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize,
long
lValue);
BOOL AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize,
double
dblValue,
int
nPrecision
=
0
,
int
nScale
=
0
);
BOOL AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize, CString strValue);
BOOL AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize, COleDateTime time);
BOOL AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize, _variant_t vtValue,
int
nPrecision
=
0
,
int
nScale
=
0
);
CString GetText()
{
return
m_strCommandText;};
BOOL Execute();
int
GetRecordsAffected()
{
return
m_nRecordsAffected;};
_CommandPtr GetCommand()
{
return
m_pCommand;};
protected
:
void
dump_com_error(_com_error
&
e);
protected
:
_CommandPtr m_pCommand;
int
m_nCommandType;
int
m_nRecordsAffected;
CString m_strCommandText;
CString m_strLastError;
DWORD m_dwLastError;
};
class
CADOException :
public
CException
{
public
:
enum
{
noError,
//
no error
Unknown,
//
unknown error
};
DECLARE_DYNAMIC(CADOException);
CADOException(
int
nCause
=
0
, CString strErrorString
=
_T(
""
));
virtual
~
CADOException();
static
int
GetError(
int
nADOError);
public
:
int
m_nCause;
CString m_strErrorString;
protected
:
};
void
AfxThrowADOException(
int
nADOError
=
1000
, CString strErrorString
=
_T(
""
));
#endif
//
AFX_ADO2_H_INCLUDED_
//
//
MODULE: Ado2.cpp
//
//
AUTHOR: Carlos Antollini
//
//
mailto: cantollini@hotmail.com
//
//
Date: 07/02/2003
//
//
Version 2.10
//
#include
"
ado2.h
"
#define
ChunkSize 100
BOOL CJetEngine::CCompactDatabase(CString strDatabaseSource, CString strDatabaseDestination)
{
try
{
::CoInitialize(NULL);
IJetEnginePtr jet(__uuidof(JetEngine));
HRESULT hr
=
jet
->
CompactDatabase(_bstr_t(strDatabaseSource.GetBuffer(
0
)), _bstr_t(strDatabaseDestination.GetBuffer(
0
)));
::CoUninitialize();
return
hr
==
S_OK;
}
catch
(_com_error
&
e)
{
::CoUninitialize();
return
FALSE;
}
}
//////////////////////////////////////////////////////
/
//
//
CADODatabase Class
//
DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs)
{
DWORD numRows
=
0
;
numRows
=
m_pRs
->
GetRecordCount();
if
(numRows
==
-
1
)
{
if
(m_pRs
->
EndOfFile
!=
VARIANT_TRUE)
m_pRs
->
MoveFirst();
while
(m_pRs
->
EndOfFile
!=
VARIANT_TRUE)
{
numRows
++
;
m_pRs
->
MoveNext();
}
if
(numRows
>
0
)
m_pRs
->
MoveFirst();
}
return
numRows;
}
BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
HRESULT hr
=
S_OK;
if
(IsOpen())
Close();
if
(strcmp(lpstrConnection, _T(
""
))
!=
0
)
m_strConnection
=
lpstrConnection;
ASSERT(
!
m_strConnection.IsEmpty());
try
{
if
(m_nConnectionTimeout
!=
0
)
m_pConnection
->
PutConnectionTimeout(m_nConnectionTimeout);
hr
=
m_pConnection
->
Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
return
hr
==
S_OK;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
void
CADODatabase::dump_com_error(_com_error
&
e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format(
"
CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n
"
,
e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription);
m_strErrorDescription
=
(LPCSTR)bstrDescription ;
m_strLastError
=
_T(
"
Connection String =
"
+
GetConnectionString()
+
'
\n
'
+
ErrorStr);
m_dwLastError
=
e.Error();
#ifdef _DEBUG
AfxMessageBox(ErrorStr, MB_OK
|
MB_ICONERROR );
#endif
}
BOOL CADODatabase::IsOpen()
{
if
(m_pConnection )
return
m_pConnection
->
GetState()
!=
adStateClosed;
return
FALSE;
}
void
CADODatabase::Close()
{
if
(IsOpen())
m_pConnection
->
Close();
}
//////////////////////////////////////////////////////
/
//
//
CADORecordset Class
//
CADORecordset::CADORecordset()
{
m_pRecordset
=
NULL;
m_pCmd
=
NULL;
m_strQuery
=
_T(
""
);
m_strLastError
=
_T(
""
);
m_dwLastError
=
0
;
m_pRecBinding
=
NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmd.CreateInstance(__uuidof(Command));
m_nEditStatus
=
CADORecordset::dbEditNone;
m_nSearchDirection
=
CADORecordset::searchForward;
}
CADORecordset::CADORecordset(CADODatabase
*
pAdoDatabase)
{
m_pRecordset
=
NULL;
m_pCmd
=
NULL;
m_strQuery
=
_T(
""
);
m_strLastError
=
_T(
""
);
m_dwLastError
=
0
;
m_pRecBinding
=
NULL;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmd.CreateInstance(__uuidof(Command));
m_nEditStatus
=
CADORecordset::dbEditNone;
m_nSearchDirection
=
CADORecordset::searchForward;
m_pConnection
=
pAdoDatabase
->
GetActiveConnection();
}
BOOL CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec,
int
nOption)
{
Close();
if
(strcmp(lpstrExec, _T(
""
))
!=
0
)
m_strQuery
=
lpstrExec;
ASSERT(
!
m_strQuery.IsEmpty());
if
(m_pConnection
==
NULL)
m_pConnection
=
mpdb;
m_strQuery.TrimLeft();
BOOL bIsSelect
=
m_strQuery.Mid(
0
, strlen(
"
Select
"
)).CompareNoCase(
"
select
"
)
==
0
&&
nOption
==
openUnknown;
try
{
m_pRecordset
->
CursorType
=
adOpenStatic;
m_pRecordset
->
CursorLocation
=
adUseClient;
if
(bIsSelect
||
nOption
==
openQuery
||
nOption
==
openUnknown)
m_pRecordset
->
Open((LPCSTR)m_strQuery, _variant_t((IDispatch
*
)mpdb, TRUE),
adOpenStatic, adLockOptimistic, adCmdUnknown);
else
if
(nOption
==
openTable)
m_pRecordset
->
Open((LPCSTR)m_strQuery, _variant_t((IDispatch
*
)mpdb, TRUE),
adOpenKeyset, adLockOptimistic, adCmdTable);
else
if
(nOption
==
openStoredProc)
{
m_pCmd
->
ActiveConnection
=
mpdb;
m_pCmd
->
CommandText
=
_bstr_t(m_strQuery);
m_pCmd
->
CommandType
=
adCmdStoredProc;
m_pConnection
->
CursorLocation
=
adUseClient;
m_pRecordset
=
m_pCmd
->
Execute(NULL, NULL, adCmdText);
}
else
{
TRACE(
"
Unknown parameter. %d
"
, nOption);
return
FALSE;
}
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
return
m_pRecordset
!=
NULL;
}
BOOL CADORecordset::Open(LPCTSTR lpstrExec,
int
nOption)
{
ASSERT(m_pConnection
!=
NULL);
ASSERT(m_pConnection
->
GetState()
!=
adStateClosed);
return
Open(m_pConnection, lpstrExec, nOption);
}
BOOL CADORecordset::OpenSchema(
int
nSchema, LPCTSTR SchemaID)
{
try
{
_variant_t vtSchemaID
=
vtMissing;
if
(strlen(SchemaID)
!=
0
)
vtSchemaID
=
SchemaID;
m_pRecordset
=
m_pConnection
->
OpenSchema((
enum
SchemaEnum)nSchema, vtMissing, vtSchemaID);
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::Requery()
{
if
(IsOpen())
{
try
{
m_pRecordset
->
Requery(adExecuteRecord);
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
return
TRUE;
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName,
double
&
dbValue)
{
double
val
=
(
double
)NULL;
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
switch
(vtFld.vt)
{
case
VT_R4:
val
=
vtFld.fltVal;
break
;
case
VT_R8:
val
=
vtFld.dblVal;
break
;
case
VT_DECIMAL:
//
Corrected by Jos?Carlos Mart韓ez Gal醤
val
=
vtFld.decVal.Lo32;
val
*=
(vtFld.decVal.sign
==
128
)
?
-
1
:
1
;
val
/=
pow(
10
, vtFld.decVal.scale);
break
;
case
VT_UI1:
val
=
vtFld.iVal;
break
;
case
VT_I2:
case
VT_I4:
val
=
vtFld.lVal;
break
;
case
VT_INT:
val
=
vtFld.intVal;
break
;
case
VT_NULL:
case
VT_EMPTY:
val
=
0
;
break
;
default
:
val
=
vtFld.dblVal;
}
dbValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex,
double
&
dbValue)
{
double
val
=
(
double
)NULL;
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
switch
(vtFld.vt)
{
case
VT_R4:
val
=
vtFld.fltVal;
break
;
case
VT_R8:
val
=
vtFld.dblVal;
break
;
case
VT_DECIMAL:
//
Corrected by Jos?Carlos Mart韓ez Gal醤
val
=
vtFld.decVal.Lo32;
val
*=
(vtFld.decVal.sign
==
128
)
?
-
1
:
1
;
val
/=
pow(
10
, vtFld.decVal.scale);
break
;
case
VT_UI1:
val
=
vtFld.iVal;
break
;
case
VT_I2:
case
VT_I4:
val
=
vtFld.lVal;
break
;
case
VT_INT:
val
=
vtFld.intVal;
break
;
case
VT_NULL:
case
VT_EMPTY:
val
=
0
;
break
;
default
:
val
=
0
;
}
dbValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName,
long
&
lValue)
{
long
val
=
(
long
)NULL;
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
if
(vtFld.vt
!=
VT_NULL
&&
vtFld.vt
!=
VT_EMPTY)
val
=
vtFld.lVal;
lValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex,
long
&
lValue)
{
long
val
=
(
long
)NULL;
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
if
(vtFld.vt
!=
VT_NULL
&&
vtFld.vt
!=
VT_EMPTY)
val
=
vtFld.lVal;
lValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, unsigned
long
&
ulValue)
{
long
val
=
(
long
)NULL;
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
if
(vtFld.vt
!=
VT_NULL
&&
vtFld.vt
!=
VT_EMPTY)
val
=
vtFld.ulVal;
ulValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex, unsigned
long
&
ulValue)
{
long
val
=
(
long
)NULL;
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
if
(vtFld.vt
!=
VT_NULL
&&
vtFld.vt
!=
VT_EMPTY)
val
=
vtFld.ulVal;
ulValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName,
int
&
nValue)
{
int
val
=
NULL;
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
switch
(vtFld.vt)
{
case
VT_BOOL:
val
=
vtFld.boolVal;
break
;
case
VT_I2:
case
VT_UI1:
val
=
vtFld.iVal;
break
;
case
VT_INT:
val
=
vtFld.intVal;
break
;
case
VT_NULL:
case
VT_EMPTY:
val
=
0
;
break
;
default
:
val
=
vtFld.iVal;
}
nValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex,
int
&
nValue)
{
int
val
=
(
int
)NULL;
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
switch
(vtFld.vt)
{
case
VT_BOOL:
val
=
vtFld.boolVal;
break
;
case
VT_I2:
case
VT_UI1:
val
=
vtFld.iVal;
break
;
case
VT_INT:
val
=
vtFld.intVal;
break
;
case
VT_NULL:
case
VT_EMPTY:
val
=
0
;
break
;
default
:
val
=
vtFld.iVal;
}
nValue
=
val;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, CString
&
strValue, CString strDateFormat)
{
CString str
=
_T(
""
);
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
switch
(vtFld.vt)
{
case
VT_R4:
str
=
DblToStr(vtFld.fltVal);
break
;
case
VT_R8:
str
=
DblToStr(vtFld.dblVal);
break
;
case
VT_BSTR:
str
=
vtFld.bstrVal;
break
;
case
VT_I2:
case
VT_UI1:
str
=
IntToStr(vtFld.iVal);
break
;
case
VT_INT:
str
=
IntToStr(vtFld.intVal);
break
;
case
VT_I4:
str
=
LongToStr(vtFld.lVal);
break
;
case
VT_UI4:
str
=
ULongToStr(vtFld.ulVal);
break
;
case
VT_DECIMAL:
{
//
Corrected by Jos?Carlos Mart韓ez Gal醤
double
val
=
vtFld.decVal.Lo32;
val
*=
(vtFld.decVal.sign
==
128
)
?
-
1
:
1
;
val
/=
pow(
10
, vtFld.decVal.scale);
str
=
DblToStr(val);
}
break
;
case
VT_DATE:
{
COleDateTime dt(vtFld);
if
(strDateFormat.IsEmpty())
strDateFormat
=
_T(
"
%Y-%m-%d %H:%M:%S
"
);
str
=
dt.Format(strDateFormat);
}
break
;
case
VT_EMPTY:
case
VT_NULL:
str.Empty();
break
;
case
VT_BOOL:
str
=
vtFld.boolVal
==
VARIANT_TRUE
?
'
T
'
:
'
F
'
;
break
;
default
:
str.Empty();
return
FALSE;
}
strValue
=
str;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex, CString
&
strValue, CString strDateFormat)
{
CString str
=
_T(
""
);
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
switch
(vtFld.vt)
{
case
VT_R4:
str
=
DblToStr(vtFld.fltVal);
break
;
case
VT_R8:
str
=
DblToStr(vtFld.dblVal);
break
;
case
VT_BSTR:
str
=
vtFld.bstrVal;
break
;
case
VT_I2:
case
VT_UI1:
str
=
IntToStr(vtFld.iVal);
break
;
case
VT_INT:
str
=
IntToStr(vtFld.intVal);
break
;
case
VT_I4:
str
=
LongToStr(vtFld.lVal);
break
;
case
VT_UI4:
str
=
ULongToStr(vtFld.ulVal);
break
;
case
VT_DECIMAL:
{
//
Corrected by Jos?Carlos Mart韓ez Gal醤
double
val
=
vtFld.decVal.Lo32;
val
*=
(vtFld.decVal.sign
==
128
)
?
-
1
:
1
;
val
/=
pow(
10
, vtFld.decVal.scale);
str
=
DblToStr(val);
}
break
;
case
VT_DATE:
{
COleDateTime dt(vtFld);
if
(strDateFormat.IsEmpty())
strDateFormat
=
_T(
"
%Y-%m-%d %H:%M:%S
"
);
str
=
dt.Format(strDateFormat);
}
break
;
case
VT_BOOL:
str
=
vtFld.boolVal
==
VARIANT_TRUE
?
'
T
'
:
'
F
'
;
break
;
case
VT_EMPTY:
case
VT_NULL:
str.Empty();
break
;
default
:
str.Empty();
return
FALSE;
}
strValue
=
str;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, COleDateTime
&
time)
{
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
switch
(vtFld.vt)
{
case
VT_DATE:
{
COleDateTime dt(vtFld);
time
=
dt;
}
break
;
case
VT_EMPTY:
case
VT_NULL:
time.SetStatus(COleDateTime::
null
);
break
;
default
:
return
FALSE;
}
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex, COleDateTime
&
time)
{
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
switch
(vtFld.vt)
{
case
VT_DATE:
{
COleDateTime dt(vtFld);
time
=
dt;
}
break
;
case
VT_EMPTY:
case
VT_NULL:
time.SetStatus(COleDateTime::
null
);
break
;
default
:
return
FALSE;
}
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName,
bool
&
bValue)
{
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
switch
(vtFld.vt)
{
case
VT_BOOL:
bValue
=
vtFld.boolVal
==
VARIANT_TRUE
?
true
:
false
;
break
;
case
VT_EMPTY:
case
VT_NULL:
bValue
=
false
;
break
;
default
:
return
FALSE;
}
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex,
bool
&
bValue)
{
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
switch
(vtFld.vt)
{
case
VT_BOOL:
bValue
=
vtFld.boolVal
==
VARIANT_TRUE
?
true
:
false
;
break
;
case
VT_EMPTY:
case
VT_NULL:
bValue
=
false
;
break
;
default
:
return
FALSE;
}
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, COleCurrency
&
cyValue)
{
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
switch
(vtFld.vt)
{
case
VT_CY:
cyValue
=
(CURRENCY)vtFld.cyVal;
break
;
case
VT_EMPTY:
case
VT_NULL:
{
cyValue
=
COleCurrency();
cyValue.m_status
=
COleCurrency::
null
;
}
break
;
default
:
return
FALSE;
}
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex, COleCurrency
&
cyValue)
{
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
switch
(vtFld.vt)
{
case
VT_CY:
cyValue
=
(CURRENCY)vtFld.cyVal;
break
;
case
VT_EMPTY:
case
VT_NULL:
{
cyValue
=
COleCurrency();
cyValue.m_status
=
COleCurrency::
null
;
}
break
;
default
:
return
FALSE;
}
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, _variant_t
&
vtValue)
{
try
{
vtValue
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::GetFieldValue(
int
nIndex, _variant_t
&
vtValue)
{
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtValue
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::IsFieldNull(LPCTSTR lpFieldName)
{
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
return
vtFld.vt
==
VT_NULL;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::IsFieldNull(
int
nIndex)
{
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
return
vtFld.vt
==
VT_NULL;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::IsFieldEmpty(LPCTSTR lpFieldName)
{
_variant_t vtFld;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value;
return
vtFld.vt
==
VT_EMPTY
||
vtFld.vt
==
VT_NULL;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::IsFieldEmpty(
int
nIndex)
{
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
try
{
vtFld
=
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value;
return
vtFld.vt
==
VT_EMPTY
||
vtFld.vt
==
VT_NULL;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::SetFieldEmpty(LPCTSTR lpFieldName)
{
_variant_t vtFld;
vtFld.vt
=
VT_EMPTY;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldEmpty(
int
nIndex)
{
_variant_t vtFld;
vtFld.vt
=
VT_EMPTY;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
DWORD CADORecordset::GetRecordCount()
{
DWORD nRows
=
0
;
nRows
=
m_pRecordset
->
GetRecordCount();
if
(nRows
==
-
1
)
{
nRows
=
0
;
if
(m_pRecordset
->
EndOfFile
!=
VARIANT_TRUE)
m_pRecordset
->
MoveFirst();
while
(m_pRecordset
->
EndOfFile
!=
VARIANT_TRUE)
{
nRows
++
;
m_pRecordset
->
MoveNext();
}
if
(nRows
>
0
)
m_pRecordset
->
MoveFirst();
}
return
nRows;
}
BOOL CADORecordset::IsOpen()
{
if
(m_pRecordset
!=
NULL
&&
IsConnectionOpen())
return
m_pRecordset
->
GetState()
!=
adStateClosed;
return
FALSE;
}
void
CADORecordset::Close()
{
if
(IsOpen())
{
if
(m_nEditStatus
!=
dbEditNone)
CancelUpdate();
m_pRecordset
->
PutSort(_T(
""
));
m_pRecordset
->
Close();
}
}
BOOL CADODatabase::Execute(LPCTSTR lpstrExec)
{
ASSERT(m_pConnection
!=
NULL);
ASSERT(strcmp(lpstrExec, _T(
""
))
!=
0
);
_variant_t vRecords;
m_nRecordsAffected
=
0
;
try
{
m_pConnection
->
CursorLocation
=
adUseClient;
m_pConnection
->
Execute(_bstr_t(lpstrExec),
&
vRecords, adExecuteNoRecords);
m_nRecordsAffected
=
vRecords.iVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::RecordBinding(CADORecordBinding
&
pAdoRecordBinding)
{
HRESULT hr;
m_pRecBinding
=
NULL;
//
Open the binding interface.
if
(FAILED(hr
=
m_pRecordset
->
QueryInterface(__uuidof(IADORecordBinding), (LPVOID
*
)
&
m_pRecBinding )))
{
_com_issue_error(hr);
return
FALSE;
}
//
Bind the recordset to class
if
(FAILED(hr
=
m_pRecBinding
->
BindToRecordset(
&
pAdoRecordBinding)))
{
_com_issue_error(hr);
return
FALSE;
}
return
TRUE;
}
BOOL CADORecordset::GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo
*
fldInfo)
{
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName);
return
GetFieldInfo(pField, fldInfo);
}
BOOL CADORecordset::GetFieldInfo(
int
nIndex, CADOFieldInfo
*
fldInfo)
{
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(vtIndex);
return
GetFieldInfo(pField, fldInfo);
}
BOOL CADORecordset::GetFieldInfo(FieldPtr pField, CADOFieldInfo
*
fldInfo)
{
memset(fldInfo,
0
,
sizeof
(CADOFieldInfo));
strcpy(fldInfo
->
m_strName, (LPCTSTR)pField
->
GetName());
fldInfo
->
m_lDefinedSize
=
pField
->
GetDefinedSize();
fldInfo
->
m_nType
=
pField
->
GetType();
fldInfo
->
m_lAttributes
=
pField
->
GetAttributes();
if
(
!
IsEof())
fldInfo
->
m_lSize
=
pField
->
GetActualSize();
return
TRUE;
}
BOOL CADORecordset::GetChunk(LPCTSTR lpFieldName, CString
&
strValue)
{
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName);
return
GetChunk(pField, strValue);
}
BOOL CADORecordset::GetChunk(
int
nIndex, CString
&
strValue)
{
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(vtIndex);
return
GetChunk(pField, strValue);
}
BOOL CADORecordset::GetChunk(FieldPtr pField, CString
&
strValue)
{
CString str
=
_T(
""
);
long
lngSize, lngOffSet
=
0
;
_variant_t varChunk;
lngSize
=
pField
->
ActualSize;
str.Empty();
while
(lngOffSet
<
lngSize)
{
try
{
varChunk
=
pField
->
GetChunk(ChunkSize);
str
+=
varChunk.bstrVal;
lngOffSet
+=
ChunkSize;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
lngOffSet
=
0
;
strValue
=
str;
return
TRUE;
}
BOOL CADORecordset::GetChunk(LPCTSTR lpFieldName, LPVOID lpData)
{
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName);
return
GetChunk(pField, lpData);
}
BOOL CADORecordset::GetChunk(
int
nIndex, LPVOID lpData)
{
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(vtIndex);
return
GetChunk(pField, lpData);
}
BOOL CADORecordset::GetChunk(FieldPtr pField, LPVOID lpData)
{
long
lngSize, lngOffSet
=
0
;
_variant_t varChunk;
UCHAR chData;
HRESULT hr;
long
lBytesCopied
=
0
;
lngSize
=
pField
->
ActualSize;
while
(lngOffSet
<
lngSize)
{
try
{
varChunk
=
pField
->
GetChunk(ChunkSize);
//
Copy the data only upto the Actual Size of Field.
for
(
long
lIndex
=
0
; lIndex
<=
(ChunkSize
-
1
); lIndex
++
)
{
hr
=
SafeArrayGetElement(varChunk.parray,
&
lIndex,
&
chData);
if
(SUCCEEDED(hr))
{
//
Take BYTE by BYTE and advance Memory Location
//
hr = SafeArrayPutElement((SAFEARRAY FAR*)lpData, &lBytesCopied ,&chData);
((UCHAR
*
)lpData)[lBytesCopied]
=
chData;
lBytesCopied
++
;
}
else
break
;
}
lngOffSet
+=
ChunkSize;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
lngOffSet
=
0
;
return
TRUE;
}
BOOL CADORecordset::AppendChunk(LPCTSTR lpFieldName, LPVOID lpData, UINT nBytes)
{
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(lpFieldName);
return
AppendChunk(pField, lpData, nBytes);
}
BOOL CADORecordset::AppendChunk(
int
nIndex, LPVOID lpData, UINT nBytes)
{
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
FieldPtr pField
=
m_pRecordset
->
Fields
->
GetItem(vtIndex);
return
AppendChunk(pField, lpData, nBytes);
}
BOOL CADORecordset::AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes)
{
HRESULT hr;
_variant_t varChunk;
long
lngOffset
=
0
;
UCHAR chData;
SAFEARRAY FAR
*
psa
=
NULL;
SAFEARRAYBOUND rgsabound[
1
];
try
{
//
Create a safe array to store the array of BYTES
rgsabound[
0
].lLbound
=
0
;
rgsabound[
0
].cElements
=
nBytes;
psa
=
SafeArrayCreate(VT_UI1,
1
,rgsabound);
while
(lngOffset
<
(
long
)nBytes)
{
chData
=
((UCHAR
*
)lpData)[lngOffset];
hr
=
SafeArrayPutElement(psa,
&
lngOffset,
&
chData);
if
(FAILED(hr))
return
FALSE;
lngOffset
++
;
}
lngOffset
=
0
;
//
Assign the Safe array to a variant.
varChunk.vt
=
VT_ARRAY
|
VT_UI1;
varChunk.parray
=
psa;
hr
=
pField
->
AppendChunk(varChunk);
if
(SUCCEEDED(hr))
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
return
FALSE;
}
CString CADORecordset::GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull,
long
numRows)
{
_bstr_t varOutput;
_bstr_t varNull(
""
);
_bstr_t varCols(
"
\t
"
);
_bstr_t varRows(
"
\r
"
);
if
(strlen(lpCols)
!=
0
)
varCols
=
_bstr_t(lpCols);
if
(strlen(lpRows)
!=
0
)
varRows
=
_bstr_t(lpRows);
if
(numRows
==
0
)
numRows
=
(
long
)GetRecordCount();
varOutput
=
m_pRecordset
->
GetString(adClipString, numRows, varCols, varRows, varNull);
return
(LPCTSTR)varOutput;
}
CString IntToStr(
int
nVal)
{
CString strRet;
char
buff[
10
];
itoa(nVal, buff,
10
);
strRet
=
buff;
return
strRet;
}
CString LongToStr(
long
lVal)
{
CString strRet;
char
buff[
20
];
ltoa(lVal, buff,
10
);
strRet
=
buff;
return
strRet;
}
CString ULongToStr(unsigned
long
ulVal)
{
CString strRet;
char
buff[
20
];
ultoa(ulVal, buff,
10
);
strRet
=
buff;
return
strRet;
}
CString DblToStr(
double
dblVal,
int
ndigits)
{
CString strRet;
char
buff[
50
];
_gcvt(dblVal, ndigits, buff);
strRet
=
buff;
return
strRet;
}
CString DblToStr(
float
fltVal)
{
CString strRet
=
_T(
""
);
char
buff[
50
];
_gcvt(fltVal,
10
, buff);
strRet
=
buff;
return
strRet;
}
void
CADORecordset::Edit()
{
m_nEditStatus
=
dbEdit;
}
BOOL CADORecordset::AddNew()
{
m_nEditStatus
=
dbEditNone;
if
(m_pRecordset
->
AddNew()
!=
S_OK)
return
FALSE;
m_nEditStatus
=
dbEditNew;
return
TRUE;
}
BOOL CADORecordset::AddNew(CADORecordBinding
&
pAdoRecordBinding)
{
try
{
if
(m_pRecBinding
->
AddNew(
&
pAdoRecordBinding)
!=
S_OK)
{
return
FALSE;
}
else
{
m_pRecBinding
->
Update(
&
pAdoRecordBinding);
return
TRUE;
}
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::Update()
{
BOOL bret
=
TRUE;
if
(m_nEditStatus
!=
dbEditNone)
{
try
{
if
(m_pRecordset
->
Update()
!=
S_OK)
bret
=
FALSE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
bret
=
FALSE;
}
if
(
!
bret)
m_pRecordset
->
CancelUpdate();
m_nEditStatus
=
dbEditNone;
}
return
bret;
}
void
CADORecordset::CancelUpdate()
{
m_pRecordset
->
CancelUpdate();
m_nEditStatus
=
dbEditNone;
}
BOOL CADORecordset::SetFieldValue(
int
nIndex, CString strValue)
{
_variant_t vtFld;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
if
(
!
strValue.IsEmpty())
vtFld.vt
=
VT_BSTR;
else
vtFld.vt
=
VT_NULL;
//
Corrected by Giles Forster 10/03/2001
vtFld.bstrVal
=
strValue.AllocSysString();
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, CString strValue)
{
_variant_t vtFld;
if
(
!
strValue.IsEmpty())
vtFld.vt
=
VT_BSTR;
else
vtFld.vt
=
VT_NULL;
//
Corrected by Giles Forster 10/03/2001
vtFld.bstrVal
=
strValue.AllocSysString();
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex,
int
nValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_I2;
vtFld.iVal
=
nValue;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName,
int
nValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_I2;
vtFld.iVal
=
nValue;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex,
long
lValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_I4;
vtFld.lVal
=
lValue;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName,
long
lValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_I4;
vtFld.lVal
=
lValue;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex, unsigned
long
ulValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_UI4;
vtFld.ulVal
=
ulValue;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, unsigned
long
ulValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_UI4;
vtFld.ulVal
=
ulValue;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex,
double
dblValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_R8;
vtFld.dblVal
=
dblValue;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName,
double
dblValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_R8;
vtFld.dblVal
=
dblValue;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex, COleDateTime time)
{
_variant_t vtFld;
vtFld.vt
=
VT_DATE;
vtFld.date
=
time;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, COleDateTime time)
{
_variant_t vtFld;
vtFld.vt
=
VT_DATE;
vtFld.date
=
time;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex,
bool
bValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_BOOL;
vtFld.boolVal
=
bValue;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName,
bool
bValue)
{
_variant_t vtFld;
vtFld.vt
=
VT_BOOL;
vtFld.boolVal
=
bValue;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex, COleCurrency cyValue)
{
if
(cyValue.m_status
==
COleCurrency::invalid)
return
FALSE;
_variant_t vtFld;
vtFld.vt
=
VT_CY;
vtFld.cyVal
=
cyValue.m_cur;
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtFld);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, COleCurrency cyValue)
{
if
(cyValue.m_status
==
COleCurrency::invalid)
return
FALSE;
_variant_t vtFld;
vtFld.vt
=
VT_CY;
vtFld.cyVal
=
cyValue.m_cur;
return
PutFieldValue(lpFieldName, vtFld);
}
BOOL CADORecordset::SetFieldValue(
int
nIndex, _variant_t vtValue)
{
_variant_t vtIndex;
vtIndex.vt
=
VT_I2;
vtIndex.iVal
=
nIndex;
return
PutFieldValue(vtIndex, vtValue);
}
BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, _variant_t vtValue)
{
return
PutFieldValue(lpFieldName, vtValue);
}
BOOL CADORecordset::SetBookmark()
{
if
(m_varBookmark.vt
!=
VT_EMPTY)
{
m_pRecordset
->
Bookmark
=
m_varBookmark;
return
TRUE;
}
return
FALSE;
}
BOOL CADORecordset::Delete()
{
if
(m_pRecordset
->
Delete(adAffectCurrent)
!=
S_OK)
return
FALSE;
if
(m_pRecordset
->
Update()
!=
S_OK)
return
FALSE;
m_nEditStatus
=
dbEditNone;
return
TRUE;
}
BOOL CADORecordset::Find(LPCTSTR lpFind,
int
nSearchDirection)
{
m_strFind
=
lpFind;
m_nSearchDirection
=
nSearchDirection;
ASSERT(
!
m_strFind.IsEmpty());
if
(m_nSearchDirection
==
searchForward)
{
m_pRecordset
->
Find(_bstr_t(m_strFind),
0
, adSearchForward,
""
);
if
(
!
IsEof())
{
m_varBookFind
=
m_pRecordset
->
Bookmark;
return
TRUE;
}
}
else
if
(m_nSearchDirection
==
searchBackward)
{
m_pRecordset
->
Find(_bstr_t(m_strFind),
0
, adSearchBackward,
""
);
if
(
!
IsBof())
{
m_varBookFind
=
m_pRecordset
->
Bookmark;
return
TRUE;
}
}
else
{
TRACE(
"
Unknown parameter. %d
"
, nSearchDirection);
m_nSearchDirection
=
searchForward;
}
return
FALSE;
}
BOOL CADORecordset::FindFirst(LPCTSTR lpFind)
{
m_pRecordset
->
MoveFirst();
return
Find(lpFind);
}
BOOL CADORecordset::FindNext()
{
if
(m_nSearchDirection
==
searchForward)
{
m_pRecordset
->
Find(_bstr_t(m_strFind),
1
, adSearchForward, m_varBookFind);
if
(
!
IsEof())
{
m_varBookFind
=
m_pRecordset
->
Bookmark;
return
TRUE;
}
}
else
{
m_pRecordset
->
Find(_bstr_t(m_strFind),
1
, adSearchBackward, m_varBookFind);
if
(
!
IsBof())
{
m_varBookFind
=
m_pRecordset
->
Bookmark;
return
TRUE;
}
}
return
FALSE;
}
BOOL CADORecordset::PutFieldValue(LPCTSTR lpFieldName, _variant_t vtFld)
{
if
(m_nEditStatus
==
dbEditNone)
return
FALSE;
try
{
m_pRecordset
->
Fields
->
GetItem(lpFieldName)
->
Value
=
vtFld;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::PutFieldValue(_variant_t vtIndex, _variant_t vtFld)
{
if
(m_nEditStatus
==
dbEditNone)
return
FALSE;
try
{
m_pRecordset
->
Fields
->
GetItem(vtIndex)
->
Value
=
vtFld;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::Clone(CADORecordset
&
pRs)
{
try
{
pRs.m_pRecordset
=
m_pRecordset
->
Clone(adLockUnspecified);
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::SetFilter(LPCTSTR strFilter)
{
ASSERT(IsOpen());
try
{
m_pRecordset
->
PutFilter(strFilter);
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::SetSort(LPCTSTR strCriteria)
{
ASSERT(IsOpen());
try
{
m_pRecordset
->
PutSort(strCriteria);
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::SaveAsXML(LPCTSTR lpstrXMLFile)
{
HRESULT hr;
ASSERT(IsOpen());
try
{
hr
=
m_pRecordset
->
Save(lpstrXMLFile, adPersistXML);
return
hr
==
S_OK;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
return
TRUE;
}
BOOL CADORecordset::OpenXML(LPCTSTR lpstrXMLFile)
{
HRESULT hr
=
S_OK;
if
(IsOpen())
Close();
try
{
hr
=
m_pRecordset
->
Open(lpstrXMLFile,
"
Provider=MSPersist;
"
, adOpenForwardOnly, adLockOptimistic, adCmdFile);
return
hr
==
S_OK;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADORecordset::Execute(CADOCommand
*
pAdoCommand)
{
if
(IsOpen())
Close();
ASSERT(
!
pAdoCommand
->
GetText().IsEmpty());
try
{
m_pConnection
->
CursorLocation
=
adUseClient;
m_pRecordset
=
pAdoCommand
->
GetCommand()
->
Execute(NULL, NULL, pAdoCommand
->
GetType());
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
void
CADORecordset::dump_com_error(_com_error
&
e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format(
"
CADORecordset Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n
"
,
e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
m_strLastError
=
_T(
"
Query =
"
+
GetQuery()
+
'
\n
'
+
ErrorStr);
m_dwLastError
=
e.Error();
#ifdef _DEBUG
AfxMessageBox( ErrorStr, MB_OK
|
MB_ICONERROR );
#endif
}
//////////////////////////////////////////////////////
/
//
//
CADOCommad Class
//
CADOCommand::CADOCommand(CADODatabase
*
pAdoDatabase, CString strCommandText,
int
nCommandType)
{
m_pCommand
=
NULL;
m_pCommand.CreateInstance(__uuidof(Command));
m_strCommandText
=
strCommandText;
m_pCommand
->
CommandText
=
m_strCommandText.AllocSysString();
m_nCommandType
=
nCommandType;
m_pCommand
->
CommandType
=
(CommandTypeEnum)m_nCommandType;
m_pCommand
->
ActiveConnection
=
pAdoDatabase
->
GetActiveConnection();
m_nRecordsAffected
=
0
;
}
BOOL CADOCommand::AddParameter(CADOParameter
*
pAdoParameter)
{
ASSERT(pAdoParameter
->
GetParameter()
!=
NULL);
try
{
m_pCommand
->
Parameters
->
Append(pAdoParameter
->
GetParameter());
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOCommand::AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize,
int
nValue)
{
_variant_t vtValue;
vtValue.vt
=
VT_I2;
vtValue.iVal
=
nValue;
return
AddParameter(strName, nType, nDirection, lSize, vtValue);
}
BOOL CADOCommand::AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize,
long
lValue)
{
_variant_t vtValue;
vtValue.vt
=
VT_I4;
vtValue.lVal
=
lValue;
return
AddParameter(strName, nType, nDirection, lSize, vtValue);
}
BOOL CADOCommand::AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize,
double
dblValue,
int
nPrecision,
int
nScale)
{
_variant_t vtValue;
vtValue.vt
=
VT_R8;
vtValue.dblVal
=
dblValue;
return
AddParameter(strName, nType, nDirection, lSize, vtValue, nPrecision, nScale);
}
BOOL CADOCommand::AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize, CString strValue)
{
_variant_t vtValue;
vtValue.vt
=
VT_BSTR;
vtValue.bstrVal
=
strValue.AllocSysString();
return
AddParameter(strName, nType, nDirection, lSize, vtValue);
}
BOOL CADOCommand::AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize, COleDateTime time)
{
_variant_t vtValue;
vtValue.vt
=
VT_DATE;
vtValue.date
=
time;
return
AddParameter(strName, nType, nDirection, lSize, vtValue);
}
BOOL CADOCommand::AddParameter(CString strName,
int
nType,
int
nDirection,
long
lSize, _variant_t vtValue,
int
nPrecision,
int
nScale)
{
try
{
_ParameterPtr pParam
=
m_pCommand
->
CreateParameter(strName.AllocSysString(), (DataTypeEnum)nType, (ParameterDirectionEnum)nDirection, lSize, vtValue);
pParam
->
PutPrecision(nPrecision);
pParam
->
PutNumericScale(nScale);
m_pCommand
->
Parameters
->
Append(pParam);
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
void
CADOCommand::SetText(CString strCommandText)
{
ASSERT(
!
strCommandText.IsEmpty());
m_strCommandText
=
strCommandText;
m_pCommand
->
CommandText
=
m_strCommandText.AllocSysString();
}
void
CADOCommand::SetType(
int
nCommandType)
{
m_nCommandType
=
nCommandType;
m_pCommand
->
CommandType
=
(CommandTypeEnum)m_nCommandType;
}
BOOL CADOCommand::Execute()
{
_variant_t vRecords;
m_nRecordsAffected
=
0
;
try
{
m_pCommand
->
Execute(
&
vRecords, NULL, adCmdStoredProc);
m_nRecordsAffected
=
vRecords.iVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
void
CADOCommand::dump_com_error(_com_error
&
e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format(
"
CADOCommand Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n
"
,
e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
m_strLastError
=
ErrorStr;
m_dwLastError
=
e.Error();
#ifdef _DEBUG
AfxMessageBox(ErrorStr, MB_OK
|
MB_ICONERROR);
#endif
}
//////////////////////////////////////////////////////
/
//
//
CADOParameter Class
//
CADOParameter::CADOParameter(
int
nType,
long
lSize,
int
nDirection, CString strName)
{
m_pParameter
=
NULL;
m_pParameter.CreateInstance(__uuidof(Parameter));
m_strName
=
_T(
""
);
m_pParameter
->
Direction
=
(ParameterDirectionEnum)nDirection;
m_strName
=
strName;
m_pParameter
->
Name
=
m_strName.AllocSysString();
m_pParameter
->
Type
=
(DataTypeEnum)nType;
m_pParameter
->
Size
=
lSize;
m_nType
=
nType;
}
BOOL CADOParameter::SetValue(
int
nValue)
{
_variant_t vtVal;
ASSERT(m_pParameter
!=
NULL);
vtVal.vt
=
VT_I2;
vtVal.iVal
=
nValue;
try
{
if
(m_pParameter
->
Size
==
0
)
m_pParameter
->
Size
=
sizeof
(
int
);
m_pParameter
->
Value
=
vtVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::SetValue(
long
lValue)
{
_variant_t vtVal;
ASSERT(m_pParameter
!=
NULL);
vtVal.vt
=
VT_I4;
vtVal.lVal
=
lValue;
try
{
if
(m_pParameter
->
Size
==
0
)
m_pParameter
->
Size
=
sizeof
(
long
);
m_pParameter
->
Value
=
vtVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::SetValue(
double
dblValue)
{
_variant_t vtVal;
ASSERT(m_pParameter
!=
NULL);
vtVal.vt
=
VT_R8;
vtVal.dblVal
=
dblValue;
try
{
if
(m_pParameter
->
Size
==
0
)
m_pParameter
->
Size
=
sizeof
(
double
);
m_pParameter
->
Value
=
vtVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::SetValue(CString strValue)
{
_variant_t vtVal;
ASSERT(m_pParameter
!=
NULL);
if
(
!
strValue.IsEmpty())
vtVal.vt
=
VT_BSTR;
else
vtVal.vt
=
VT_NULL;
//
Corrected by Giles Forster 10/03/2001
vtVal.bstrVal
=
strValue.AllocSysString();
try
{
if
(m_pParameter
->
Size
==
0
)
m_pParameter
->
Size
=
sizeof
(
char
)
*
strValue.GetLength();
m_pParameter
->
Value
=
vtVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::SetValue(COleDateTime time)
{
_variant_t vtVal;
ASSERT(m_pParameter
!=
NULL);
vtVal.vt
=
VT_DATE;
vtVal.date
=
time;
try
{
if
(m_pParameter
->
Size
==
0
)
m_pParameter
->
Size
=
sizeof
(DATE);
m_pParameter
->
Value
=
vtVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::SetValue(_variant_t vtValue)
{
ASSERT(m_pParameter
!=
NULL);
try
{
if
(m_pParameter
->
Size
==
0
)
m_pParameter
->
Size
=
sizeof
(VARIANT);
m_pParameter
->
Value
=
vtValue;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::GetValue(
int
&
nValue)
{
_variant_t vtVal;
int
nVal
=
0
;
try
{
vtVal
=
m_pParameter
->
Value;
switch
(vtVal.vt)
{
case
VT_BOOL:
nVal
=
vtVal.boolVal;
break
;
case
VT_I2:
case
VT_UI1:
nVal
=
vtVal.iVal;
break
;
case
VT_INT:
nVal
=
vtVal.intVal;
break
;
case
VT_NULL:
case
VT_EMPTY:
nVal
=
0
;
break
;
default
:
nVal
=
vtVal.iVal;
}
nValue
=
nVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::GetValue(
long
&
lValue)
{
_variant_t vtVal;
long
lVal
=
0
;
try
{
vtVal
=
m_pParameter
->
Value;
if
(vtVal.vt
!=
VT_NULL
&&
vtVal.vt
!=
VT_EMPTY)
lVal
=
vtVal.lVal;
lValue
=
lVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::GetValue(
double
&
dbValue)
{
_variant_t vtVal;
double
dblVal;
try
{
vtVal
=
m_pParameter
->
Value;
switch
(vtVal.vt)
{
case
VT_R4:
dblVal
=
vtVal.fltVal;
break
;
case
VT_R8:
dblVal
=
vtVal.dblVal;
break
;
case
VT_DECIMAL:
//
Corrected by Jos?Carlos Mart韓ez Gal醤
dblVal
=
vtVal.decVal.Lo32;
dblVal
*=
(vtVal.decVal.sign
==
128
)
?
-
1
:
1
;
dblVal
/=
pow(
10
, vtVal.decVal.scale);
break
;
case
VT_UI1:
dblVal
=
vtVal.iVal;
break
;
case
VT_I2:
case
VT_I4:
dblVal
=
vtVal.lVal;
break
;
case
VT_INT:
dblVal
=
vtVal.intVal;
break
;
case
VT_NULL:
case
VT_EMPTY:
dblVal
=
0
;
break
;
default
:
dblVal
=
0
;
}
dbValue
=
dblVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::GetValue(CString
&
strValue, CString strDateFormat)
{
_variant_t vtVal;
CString strVal
=
_T(
""
);
try
{
vtVal
=
m_pParameter
->
Value;
switch
(vtVal.vt)
{
case
VT_R4:
strVal
=
DblToStr(vtVal.fltVal);
break
;
case
VT_R8:
strVal
=
DblToStr(vtVal.dblVal);
break
;
case
VT_BSTR:
strVal
=
vtVal.bstrVal;
break
;
case
VT_I2:
case
VT_UI1:
strVal
=
IntToStr(vtVal.iVal);
break
;
case
VT_INT:
strVal
=
IntToStr(vtVal.intVal);
break
;
case
VT_I4:
strVal
=
LongToStr(vtVal.lVal);
break
;
case
VT_DECIMAL:
{
//
Corrected by Jos?Carlos Mart韓ez Gal醤
double
val
=
vtVal.decVal.Lo32;
val
*=
(vtVal.decVal.sign
==
128
)
?
-
1
:
1
;
val
/=
pow(
10
, vtVal.decVal.scale);
strVal
=
DblToStr(val);
}
break
;
case
VT_DATE:
{
COleDateTime dt(vtVal);
if
(strDateFormat.IsEmpty())
strDateFormat
=
_T(
"
%Y-%m-%d %H:%M:%S
"
);
strVal
=
dt.Format(strDateFormat);
}
break
;
case
VT_EMPTY:
case
VT_NULL:
strVal.Empty();
break
;
default
:
strVal.Empty();
return
FALSE;
}
strValue
=
strVal;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::GetValue(COleDateTime
&
time)
{
_variant_t vtVal;
try
{
vtVal
=
m_pParameter
->
Value;
switch
(vtVal.vt)
{
case
VT_DATE:
{
COleDateTime dt(vtVal);
time
=
dt;
}
break
;
case
VT_EMPTY:
case
VT_NULL:
time.SetStatus(COleDateTime::
null
);
break
;
default
:
return
FALSE;
}
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
BOOL CADOParameter::GetValue(_variant_t
&
vtValue)
{
try
{
vtValue
=
m_pParameter
->
Value;
return
TRUE;
}
catch
(_com_error
&
e)
{
dump_com_error(e);
return
FALSE;
}
}
void
CADOParameter::dump_com_error(_com_error
&
e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format(
"
CADOParameter Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n
"
,
e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
m_strLastError
=
ErrorStr;
m_dwLastError
=
e.Error();
#ifdef _DEBUG
AfxMessageBox(ErrorStr, MB_OK
|
MB_ICONERROR);
#endif
}
IMPLEMENT_DYNAMIC(CADOException, CException)
CADOException::CADOException(
int
nCause, CString strErrorString) : CException(TRUE)
{
m_nCause
=
nCause;
m_strErrorString
=
strErrorString;
}
CADOException::
~
CADOException()
{
}
int
CADOException::GetError(
int
nADOError)
{
switch
(nADOError)
{
case
noError:
return
CADOException::noError;
break
;
default
:
return
CADOException::Unknown;
}
}
void
AfxThrowADOException(
int
nADOError, CString strErrorString)
{
throw
new
CADOException(nADOError, strErrorString);
}
演示代码:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#include <ado2.h>
int main()
{
// open
CADODatabase database;
CString strConnectionString;
strConnectionString.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False;", "C:\\a.mdb");
database.SetConnectionString(strConnectionString);
BOOL bResult = database.Open();
if (!bResult)
{
CString strError = "测试数据库服务器连接失败!错误原因:\r\n\r\n\r\n";
strError += database.GetLastErrorString();
cout <<strError.GetBuffer(0) << endl;
}else
{
cout << "connect ok" << endl;
}
// add new
BOOL bRet;
CADORecordset rs(&database);
CString quryStr;
quryStr.Format("ttt");
bRet = rs.Open((LPCTSTR)quryStr);
rs.AddNew();
rs.SetFieldValue("test1", (CString)("strRecordID"));
rs.SetFieldValue("test2", (CString)("strTruckID"));
bRet = rs.Update();
rs.Close();
// update
quryStr.Format("select * from ttt where 编号 = 1");
rs.Open((LPCTSTR)quryStr);
if (rs.GetRecordCount() == 1)
{
rs.Edit();
rs.SetFieldValue("test1", (CString)"abc");
rs.Update();
}
rs.Close();
//
quryStr.Format("SELECT * FROM ttt");
bRet = rs.Open((LPCTSTR)quryStr);
if (!bRet)
{
cout << "Open Error" << endl;
}
while (!rs.IsEof())
{
CString strCoalNo;
rs.GetFieldValue("test1", strCoalNo);
cout << (LPCTSTR)strCoalNo << endl;
rs.MoveNext();
}
rs.Close();
// delete
quryStr.Format( "SELECT * FROM ttt");
rs.Open((LPCTSTR)quryStr);
rs.Delete();
database.Close();
return 0;
}