一)
如果我用这样做可以增加,没问题
_variant_t vt("hello");
m_pRecordset->PutCollect(_variant_t(index), vt);二)
如果这么做就插不进去,
CString value;
value = "hello"; //或是value.Format("hello");
vt.vt = value.IsEmpty() ? VT_NULL : VT_BSTR;
vt.bstrVal = value.AllocSysString();
m_pRecordset->PutCollect(_variant_t(index), vt);我想问题是出在_variant_t对象的构造上,我不知道这两种方法有什么区别?大家帮帮我,多谢了!另外:三)
const char* pszText = "hello";
_variant_t vt(pszText);
_pRecordset->PutCollect(_variant_t(index), vt);这样做也没问题,但是当我把"hello"字符串通过函数参数传进去,又不行了?
比如四)
void AddRecord(long index, const char* pszText)
{
_variant_t vt(pszText);
_pRecordset->PutCollect(_variant_t(index), vt);
}多谢了!

解决方案 »

  1.   

    显式设置vt的类型为字符串,例如
    vt.type = VT_BSTR
    你直接做_variant_t vt("hello")时,它的构造函数会给你做这个
    但是你直接vt.bstrVal = value.AllocSysString(),那么它的数据类型没有被设置
    仍然是初始值
      

  2.   

    你可以去察看_variant_t的定义,以及它的构造函数的实现代码,这些MFC带的源代码里都有。
      

  3.   

    vt.vt = value.IsEmpty() ? VT_NULL : VT_BSTR; //此处 value为IsEmpty() 故为VT_NULL
    vt.bstrVal = value.AllocSysString();
    m_pRecordset->PutCollect(_variant_t(index), vt);
      

  4.   

    楼上的是什么意思?vt.vt = VT_NULL 或 vt.vt = VT_BSTR
    都不行;而且value是不空的,他的内容为"hello"啊
      

  5.   

    类型定义成bstr="hello";
    再insert into 就可以了。
      

  6.   

    你的四个应该都没有错
    就第二个没有用过void AddRecord(long index, const char* pszText)
    {
    _variant_t vt(pszText);
    _pRecordset->PutCollect(_variant_t(index), vt);
    }
    我是写成_pRecordset->PutCollect(_variant_t((long)index), _variant_t(pszText));
      

  7.   

    终于明白了,我用的数据库是Access97是,连接字串中的Provider项应该是:
    Provider=Microsoft.Jet.OLEDB.3.51;  而我用的是
    Provider=Microsoft.Jet.OLEDB.4.0;