VC + + solution to return null error when connecting database query via ADO

Source: Internet
Author: User

When the field value of the database is allowed to be empty, and the content is also empty at this time, execution of the query will cause an error, such as

CString str = precordset->getfields ()->getitem ((long) 0)->getvalue ();

Or

str=precordset->getcollect ("Posinfo");

Will pop up the following window prompt error!

even more strange is the catch (...) And I can't catch the exception .
I got a big head on a problem today and finally got it.

Cause of error: After GETCOLLCT returned null because STR is a CString object compiler automatically converts _varint_t to CString and _varint_t is empty so the conversion failed is not a database exception and is not an exception at all. catch (...) Unable to seize.

Solution Solutions

In a potentially empty place, add the following code

_variant_t var;  Var= m_precordset->getcollect ("Posinfo");          May be empty if (var.vt! = vt_null)   //Is null {         strposinfo = var;  }  else {        strposinfo = "";}

You'll be able to detect null values.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.