ADOのITEMプロパティ
#import
"c://program files//common files//system//ado//msado15.dll" rename("EOF","adoEOF")
using
namespace ADODB ;
int
_tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
CoInitialize( NULL ) ;
{
_bstr_t bstrConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
"Dbq=D://Olympus//Capusule//EN//SimulateDB.mdb;Uid=;Pwd=;");
_ConnectionPtr connPtr ;
connPtr.CreateInstance("ADODB.Connection") ;
HRESULT hResult = connPtr->Open( bstrConnection , "" , "" ,
adConnectUnspecified ) ;
ASSERT( SUCCEEDED( hResult ) ) ;
_CommandPtr cmdPtr ;
cmdPtr.CreateInstance("ADODB.Command") ;
cmdPtr->ActiveConnection = connPtr ;
cmdPtr->CommandText = "select * from patient where id = ? ";
cmdPtr->CommandType = adCmdText ;
cmdPtr->Prepared = true ;
_ParameterPtr firstParam = cmdPtr->CreateParameter( "1" , adChar ,
adParamInput , 10 , "id1" ) ;
cmdPtr->Parameters->Append( firstParam ) ;
_RecordsetPtr cntRsPtr = cmdPtr->Execute( NULL , NULL , adCmdText ) ;
cntRsPtr->MoveFirst() ;
while ( !cntRsPtr->adoEOF )
{
int nCnt = cntRsPtr->Fields->Count ;
_variant_t var ;
_variant_t id ;
id.vt = VT_INT ;
id.llVal = 1 ;
FieldPtr idPtr = cntRsPtr->Fields->Item["ID"] ;
var = idPtr->Value ;
//idPtr = cntRsPtr->Fields->Item[(short)1] ;
// ok
idPtr = cntRsPtr->Fields->Item[(long)1] ; // ok
idPtr = cntRsPtr->Fields->Item[(int)1] ; // fail
//
throw invalid arg errors.
var = idPtr->Value ;
_variant_t varID = 0 ;
idPtr = cntRsPtr->Fields->Item["birthday"] ;
_variant_t birth = idPtr->Value ;
SYSTEMTIME time ;
VariantTimeToSystemTime( birth.date , &time ) ;
cntRsPtr->MoveNext() ;
}
}
CoUninitialize() ;
}
説明:
1、ADOを利用して、DBアクセスする
2、スマートポインタ( smart pointer)を利用して、インタフェース漏れを防ぐ
3、CoUninitializeを呼び出し前に、ADOオブジェクトを解放しなければならない。CoInitializeとCoUninitializeの間に、{}を置いた。そのうちに、オブジェクトのdescructorが発生する
4、コンパイルしたstatementを利用して、効率が高いになる
5、
_RecordsetPtr
のFieldsのITEMをアクセスするとき、_bstr_tとshort、long二つ種類のIndexが利用できます。注意すべきのはintがindexとして利用できない。何故なから:
The answer is actually simpler. ADO was designed from the beginning to support "higher-level languages", which at the time meant VB/VBA.
In spite of the "Universal" intent of ADO it was essentially designed for VB. That is why practically every value in ADO is a Variant. ADO also manages a bit of parameter polymorphism by checking the type of received variants and performing alternative handling based on it. While VB supports Variants,
only a subset of the types available
(VarEnum) are included. A
system integer
,
anything unsigned
, and many other types are not included, thus throw invalid arg errors.
If you lookup the VarType Function in the VB help files you will see a list
of 'acceptable' values.
of 'acceptable' values.
6、参考
VARTYPE
An enumeration type used in VARIANT, TYPEDESC, OLE property sets, and safe arrays.
The enumeration constants listed in the following VARENUM section are valid in the vt field of a VARIANT structure.
typedef unsigned short VARTYPE;
enum VARENUM{
VT_EMPTY = 0, // Not specified.
VT_NULL = 1, // Null.
VT_I2 = 2, // 2-byte signed int.
VT_I4 = 3, // 4-byte signed int.
VT_R4 = 4, // 4-byte real.
VT_R8 = 5, // 8-byte real.
VT_CY = 6, // Currency.
VT_DATE = 7, // Date.
VT_BSTR = 8, // Binary string.
VT_DISPATCH = 9, // IDispatch
VT_ERROR = 10, // Scodes.
VT_BOOL = 11, // Boolean; True=-1, False=0.
VT_VARIANT = 12, // VARIANT FAR*.
VT_UNKNOWN = 13, // IUnknown FAR*.
VT_UI1 = 17, // Unsigned char.
// Other constants that are not valid in VARIANTs omitted here.
};
VT_RESERVED = (int) 0x8000
// By reference, a pointer to the data is passed.
VT_BYREF = (int) 0x4000
VT_ARRAY = (int) 0x2000 // A safe array of the data is passed.
7、備考
Short
VT_I2 2
Long
VT_I4 3
Int
VT_INT 22 ( not included in VBの
VARENUM)
VC
のVARENUM定義にさらに多くのマクロがある