ADOのITEMプロパティ

原创 2007年09月27日 15:59:00

 

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.
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 VBVARENUM)
VCのVARENUM定義にさらに多くのマクロがある
 

相关文章推荐

RecyclerView 的使用(2)の多Item布局

上一篇介绍的了RecyclerView的基础使用http://blog.csdn.net/leejizhou/article/details/50670657,这一篇给大家介绍下如何利用Recycle...

Recyclerview中可以任意拖拽item的demo

  • 2017年11月09日 14:47
  • 42.5MB
  • 下载

初涉三层架构,UI层和DAL层的调用,ADO.NET对数据库的数据增删改

UI层:可以说是界面层吧

C#二十六 使用Ado.Net调用存储过程

存储过程是连接式访问数据库的一种延伸,主要是通过命令对象调用数据库系统中的存储过程来完成的。存储过程可以带参数,也可以不带参数,可以返回结果页可以没有返回结果。存储过程执行速度快、允许模块化程序设计并...

解决VS2013使用MySQL无法创建ADO.NET实体数据类型的问题

VS2013使用MySQL无法创建ADO.NET实体数据类型的问题 安装connector和vs插件之后,在添加ado.net实体时,提示项目使用了最新版本实体框架;但是,找不到进行数据连接所需的与此...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ADOのITEMプロパティ
举报原因:
原因补充:

(最多只允许输入30个字)