我用ADO 连接Acess数据库时,执行添加时,当有的栏位为空时,程序就出错,而数据库里是该栏位是允许为空的,为什么啊?代码如下:
m_pRecUsers->AddNew();
m_pRecUsers->PutCollect("Name", _variant_t(m_Name));
m_pRecUsers->PutCollect("IDCard", _variant_t(m_ICCard));
m_pRecUsers->PutCollect("Sex", _variant_t(m_Sex));
m_pRecUsers->PutCollect("Age", long(atoi(m_Age)));
m_pRecUsers->Update();

解决方案 »

  1.   

    当库中某字段为文本类型时,如果为空时必须作一下处理!
    _variant_t vName,vICCard,vSex,vAge;
    if(m_Name=="")
       vName.vt=VT_NULL;
    else
       vName=m_Name;
    if(m_ICCard=="")
       vICCard.vt=VT_NULL;
    else
       vICCard=m_ICCard;
    if(m_Sex=="")
       vSex.vt=VT_NULL;
    else
       vSex=m_Sex;
    m_pRecUsers->AddNew();
    m_pRecUsers->PutCollect("Name", vName);
    m_pRecUsers->PutCollect("IDCard",vICCard);
    m_pRecUsers->PutCollect("Sex", vSex);
    m_pRecUsers->PutCollect("Age", long(atoi(m_Age)));
    m_pRecUsers->Update();
    【注】
      楼主定义的库中字段应该是Name、IDCard、Sex是文本,Age是整型吧?我是这么认为的。
      如果有什么不对的话,记着字段为文本类型的要做以上处理,如果为数字型就不用了,因为库会默认为0的。
      

  2.   

    zlxcjy(~您的朋友~) :
          你好,谢谢你,Very Much了,我试了一下,可以。还想问个相关问题,就是_variant_t的使用,我在从数据库中向外取时,一般怎样用?
      

  3.   

    给个例子:
    vName=m_pRecUsers->GetCollect("Name");
    vICCard=m_pRecUsers->GetCollect("IDCard");
    vSex=m_pRecUsers->GetCollect("Sex");
    vAge=m_pRecUsers->GetCollect("Age");CString m_name=(LPCTSTR)(_bstr_t)vName;
    CString m_Iccard=(LPCTSTR)(_bstr_t)vICCard;
    CString m_sex=(LPCTSTR)(_bstr_t)vSex;
    int age=vAge.lVal;//当为整型时如果要转化整型为字符串型CString m_age.Format("%d",age);
    【注】上面列出了当为字符串、整型的情况,如果为float scope=vScope.fltVal;