VC下sql+ado数据库v_strTemp.vt == VT_NULL空值处理

探讨VC环境下使用ADO操作数据库时,如何正确处理数据库中的NULL值,避免因空值导致的数据读取错误。

转自:http://blog.youkuaiyun.com/adu285041555/article/details/8670261

VC下sql+ado数据库v_strTemp.vt == VT_NULL判断的问题

C/C++ code
    for (int i=0;i<q;i++)  
    {  
        str.Format("%d",i+1); //行数从1开始 
        m_list.InsertItem(i, str);//插入行号
        for(int j=0; j<fieldCnt; j++) //fieldCnt为字段个数
        {   
            
            v_strTemp=pRst->GetCollect((long)j);//获取第j列内容
/////////////////////////////////////////////////////////////////
     //判断数据库中的NULL值 
            if (v_strTemp.vt == VT_NULL) //空判断很重要!!!否则遇以空时处理会出问题
            {
                str ="NULL";
            }
            else
            {
                str = (LPCSTR)(_bstr_t)v_strTemp;
            }
///////////////////////////////////////////////////////////////////
            m_list.SetItemText(i,j+1,str);//插入内容  
        }
         pRst->MoveNext();
    }  


我很不解,代码不加以上的空判部分,当由pRst->GetCollect((long)j)获取到的数据库中的某项为空值时,
竟然会直接退出里层的for循环!
比如假设fieldCnt=5
数据库表的内容为:
姓名 年龄  学校 兴趣  备注
aa    bb   null  dd   ee  

如果不加空判断,当遍历到学校为null时,也就是当j=1时,就直接退出里层部分的for循环了,
导致我住列表控件里插入内容时,只能插入aa和bb,后面的dd和ee就没法插入了!
但是加上后就能正常遍历!这到底是为什么啊?这其中的原理是什么?



------解决方案--------------------------------------------------------
v_strTemp=pRst->GetCollect((long)j);//获取第j列内容
问题出在这里。GetCollect的参数是_bstr_r类型。

int CHLD_TERMINATEDlg::OnGenerateIniFile() // 生成配置文件 { // TODO: Add your control notification handler code here // 打开Ini文件,按编辑框中的内容写入INI文件 CStdioFile file; CString strSection, strKey, strKeyValue, m_strPath; if (m_sUpFilePath == "") { MessageBox("请选择程序文件!", "提示", MB_OK | MB_ICONWARNING); return FALSE; } // if (m_sReleaseDate.GetLength() < 6) // { // MessageBox("请输入6位的终端软件发布日期:日月年!", "提示", MB_OK | MB_ICONWARNING); // return FALSE; // } // if (m_sTermArea.GetLength() < 4) // { // MessageBox("请输入4位区号码!", "提示", MB_OK | MB_ICONWARNING); // return FALSE; // } UpdateData(TRUE); gProfileInfo.packetlen = atoi(m_sPacketLen); // 帧长度,由界面设置 if (gProfileInfo.lFilelen % gProfileInfo.packetlen) gProfileInfo.nPageNum = gProfileInfo.lFilelen / gProfileInfo.packetlen + 1; else gProfileInfo.nPageNum = gProfileInfo.lFilelen / gProfileInfo.packetlen; m_ctrlProgBar.SetRange(0, gProfileInfo.nPageNum); strcpy(gProfileInfo.fileName, (LPCSTR)m_sFileName); strcpy(gProfileInfo.softVersion, (LPCSTR)m_sSoftVersion); strcpy(gProfileInfo.confInfoCode, (LPCSTR)m_sCapacityCode); strcpy(gProfileInfo.factoryCode, (LPCSTR)m_sFactoryCode); strcpy(gProfileInfo.deviceNo, (LPCSTR)m_sDeviceCode); m_strPath = ".\\windy.ini"; // 生成配置文件 if (file.Open(m_strPath, CFile::modeCreate | CFile::typeText | CFile::modeReadWrite)) { file.WriteString("[config]\r\n"); file.Close(); } else MessageBox("文件生成失败!", "错误", MB_ICONERROR | MB_OK); strSection = "config"; strKey = "trans_len"; strKeyValue.Format("%d", gProfileInfo.packetlen); WritePrivateProfileString((LPCTSTR)strSection, (LPCTSTR)strKey, (LPCTSTR)strKeyValue, (LPCTSTR)m_strPath); strKey = "file_name"; strKeyValue.Format("%s", gProfileInfo.fileName); WritePrivateProfileString((LPCTSTR)strSection, (LPCTSTR)strKey, (LPCTSTR)strKeyValue, (LPCTSTR)m_strPath); strKey = "file_crc"; strKeyValue.Format("%02X%02X", gProfileInfo.fileCrc[0] & 0XFF, gProfileInfo.fileCrc[1] & 0XFF); WritePrivateProfileString((LPCTSTR)strSection, (LPCTSTR)strKey, (LPCTSTR)strKeyValue, (LPCTSTR)m_strPath); strKey = "file_info"; // gProfileInfo.factoryCode[4] = 0; // gProfileInfo.deviceNo[8] = 0; // gProfileInfo.softVersion[4] = 0; // gProfileInfo.releaseDate[3] = 0; // gProfileInfo.confInfoCode[11] = 0; // zyh 2011-10-10 begin memcpy(gProfileInfo.factoryCode, m_sFactoryCode, 4); gProfileInfo.factoryCode[4] = 0; memcpy(gProfileInfo.deviceNo, m_sDeviceCode, 8); gProfileInfo.deviceNo[8] = 0; memcpy(gProfileInfo.softVersion, m_sSoftVersion, 4); gProfileInfo.softVersion[4] = 0; //====zyh 2011-10-10 begin CString strTemp = m_sReleaseDate; char temp[16]; strcpy(temp, strTemp.Mid(strTemp.GetLength() - 6, 2)); char *stopstring = temp + 2; gProfileInfo.releaseDate[0] = strtol(temp, &stopstring, 16); strcpy(temp, strTemp.Mid(strTemp.GetLength() - 4, 2)); stopstring = temp + 2; gProfileInfo.releaseDate[1] = strtol(temp, &stopstring, 16); strcpy(temp, strTemp.Mid(strTemp.GetLength() - 2, 2)); stopstring = temp + 2; gProfileInfo.releaseDate[2] = strtol(temp, &stopstring, 16); gProfileInfo.releaseDate[3] = 0; //====zyh 2011-10-10 end memcpy(gProfileInfo.confInfoCode, m_sCapacityCode, 11); gProfileInfo.confInfoCode[11] = 0; // zyh 2011-10-10 end strKeyValue.Format("%s%s%s%s%s", gProfileInfo.factoryCode, gProfileInfo.deviceNo, gProfileInfo.softVersion, gProfileInfo.releaseDate, gProfileInfo.confInfoCode); WritePrivateProfileString((LPCTSTR)strSection, (LPCTSTR)strKey, (LPCTSTR)strKeyValue, (LPCTSTR)m_strPath); strKey = "file_len"; strKeyValue.Format("%d", gProfileInfo.lFilelen); WritePrivateProfileString((LPCTSTR)strSection, (LPCTSTR)strKey, (LPCTSTR)strKeyValue, (LPCTSTR)m_strPath); strKey = "file_mes"; strKeyValue = gProfileInfo.fileMess; WritePrivateProfileString((LPCTSTR)strSection, (LPCTSTR)strKey, (LPCTSTR)strKeyValue, (LPCTSTR)m_strPath); strKey = "file_israr"; strKeyValue = "0"; WritePrivateProfileString((LPCTSTR)strSection, (LPCTSTR)strKey, (LPCTSTR)strKeyValue, (LPCTSTR)m_strPath); // MessageBox("配置文件生成完毕!", "提示", MB_OK | MB_ICONWARNING); m_tabstyle.EnableWindow(TRUE); m_ctrl_Com_Port.EnableWindow(TRUE); m_ctrl_Com_Baud.EnableWindow(TRUE); m_ctrl_Local_Port.EnableWindow(TRUE); m_btnOpen.EnableWindow(TRUE); return TRUE; }请详细解释一下每一行代码
最新发布
09-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值