在看到VC下用ADO访问数据库的时候遇到的_vatiant_t,后发现他是一个结构体,为什么从数据库里GetCollect出的数据要先存在用_variant_t定义的变量中,然后用(_bstr_t)作强制类型转换才能存入CString,有的时候还要(LPCTSTR)(_bstr_t)
ex: CString csTemp;
csTemp = (LPCTSTR)(_bstr_t)(m_pTRecordset->GetCollect("ID"));
不解,不懂COM,希望能得到指教。还有一个,Recordset的Open方法有5个参数recordset.Open (Source, ActiveConnection, CursorType, LockType, Options)
程序中:
m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
 第二个参数ActiveConnection的解释实在没看懂,还望高手赐教 谢谢

解决方案 »

  1.   

    _variant_t是为了解决com数据可移植性而设的。ActiveConnection就是数据库的连接,在取数据集之前肯定要先保证已连接数据库。
      

  2.   

    能不能更具体些 详细些 _variant_t((IDispatch*)theApp.m_pConnection,true)这个为什么要用IDispatch*强制转换,theApp.m_Connection是什么,TRUE 
      

  3.   

    1._variant_t是一个复合类型,一方面为了COM的可移植性,另一方面,从数据库中取得的数据不一定都是string或者int,
    使用_variant_t是为了统一接口返回类型。2.MSDN中的解释。
    _variant_t( IDispatch* pDispSrc, bool fAddRef = true )   
    Constructs a _variant_t object of type VT_DISPATCH from a COM interface pointer. If fAddRef is true, then AddRef is called on the supplied interface pointer to match the call to Release that will occur when the _variant_t object is destroyed. It is up to you to call Release on the supplied interface pointer. If fAddRef is false, this constructor takes ownership of the supplied interface pointer; do not call Release on the supplied interface pointer. 
      

  4.   

    那_bstr_t和 LPCTSTR  两个类型是怎么回事
    这段英文解释我没看懂-_-!,是说从一个COM接口指针构造的一个VT_DISPATCH类型的_variant_t对象(什么意思。。) call release on the supplied interface pointer 调用释放提供的接口指针 怎么理解