Home > Database > Mysql Tutorial > body text

VC++ 通过ADO连接数据库查询时返回空值报错的解决方案

WBOY
Release: 2016-06-07 15:48:17
Original
1120 people have browsed it

当数据库的字段允许为空时, 而且此时内容也为空时, 则执行查询会出错,例如 CString str = pRecordset-GetFields()-GetItem((long)0)-GetValue(); 或者 str = pRecordset -GetCollect(posInfo); 会弹出如下窗口提示出错! 更加奇怪的是 catch(...)也抓不到

当数据库的字段值允许为空时, 而且此时内容也为空时,则执行查询会出错,例如

CString str = pRecordset->GetFields()->GetItem((long)0)->GetValue();

或者

str= pRecordset->GetCollect("posInfo");   

会弹出如下窗口提示出错!

VC++ 通过ADO连接数据库查询时返回空值报错的解决方案     

更加奇怪的是  catch(...)也抓不到异常
今天碰着个问题算是头弄大了  最后终于弄好了

报错的原因:   在GetCollct返回了NULL之后   由于str是一个CString对象  编译器自动将_varint_t转换成CString  而此时_varint_t为空  因此转换失败  不是数据库的异常 而且根本就不是异常 所以  catch(…)无法抓住。

解决方案 

在可能为空的地方,加入如下代码

 _variant_t var;
  var= m_pRecordset->GetCollect("posInfo");          //可能为空
 if(var.vt != VT_NULL)   //为NULL
 {
         strPosInfo = var;
  }
  else
 {
        strPosInfo = “”;
}
Copy after login

就可以检测NULL值了


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!