Detecting no results in adodb select query
I'm using ADODB connection in C. The code works more or less fine but I'm getting errors when there is no result for my query and I try to read it. Relevant code:
__object *con, *rec;
con = __object_create("ADODB.Connection");
if(con!=NULL) con->Open("odbc name");
if (con == NULL || con->State==0)
{
return 0;
}
rec= __object_create("ADODB.RecordSet");
sprintf(query, "SELECT SUM(column) FROM table WHERE %s", constraint);
rec->CursorLocation=3;
rec->Open(query, con, 1, 3);
float result = rec->Fields(0); // <- Error here
rec->Close();
__object_delete(rec);
__object_delete(con);
I'm getting error code 80020005 (Type mismatch). The DB column is type float. When there are records that meet the constraint and I get a result everything works fine. But when it matches no records the DB server returns null and I get the error. Fortunately result is set to 0 which is reasonable but I would like to detect this better.
The standard ways (BOF/EOF, Fields->Count, == NULL, ...) all fail. Most开发者_运维百科 code samples I can find are for VB and not C so they are not really helpful.
rec->Fields(0).Value
returns a variant, before assigning to the float variable check if the variant represents a VT_NULL which indicates that the data returned is NULL.
精彩评论