如何在ADO中读取NULL值
How to read NULL value in ADO
我有一个与NULL values
相关的问题,我通过ADO连接到SQL Server 2008,如下代码:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <ole2.h>
#include <oleauto.h>
#ifdef _MSC_VER
#import "c:Archivos de programaArchivos comunesSystemadomsado15.dll" rename ("EOF","adoEOF") no_namespace
#else
#define V_INT(X) V_UNION(X, intVal)
#define V_UINT(X) V_UNION(X, uintVal)
#include "msado15.tlh"
#endif
#include <comutil.h>
struct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} InitOle_tag;
//------------------ utility fns to simplify access to recordset fields
_bstr_t RsItem( _RecordsetPtr p, BSTR fldName )
{ // by field name
return( p->Fields->Item[_variant_t(fldName)]->Value );
}
_bstr_t RsItem( _RecordsetPtr p, long nIdx )
{ // by field # (0 is first)
return( p->Fields->Item[_variant_t(nIdx)]->Value );
}
//-------------------------------- The Program ----------------
int main()
{
_RecordsetPtr spRs;
HRESULT hr;
_bstr_t sConn= "driver={sql server};SERVER=VIRTUALPC;Database=test;UID=sa; PWD=";
_bstr_t sSQL= "SELECT att0 FROM [dbo].[mytable]";
try
{
hr= spRs.CreateInstance( __uuidof(Recordset) );
if FAILED(hr) printf("CreateInstance failedn");
hr= spRs->Open( sSQL, sConn, adOpenForwardOnly, adLockReadOnly, adCmdText );
if FAILED(hr) printf("Open failedn");
while( !(spRs->adoEOF) ) {
printf("%sn",
(char*) RsItem( spRs, 0L )
);
spRs->MoveNext();
}
spRs->Close();
} catch( _com_error &e) {
printf("Error:%sn",(char*)e.Description());
}
return 0;
}
我正在阅读的att0
专栏是这样的:
att0
----
477
113
466
527
NULL
NULL
NULL
执行程序后,我得到:
477
113
466
527
Error:(null)
Press any key to continue . . .
我希望当它检测到NULL值时,程序显示
477
113
466
527
-1
-1
-1
有什么想法吗?
所有这些工作真的很棒,但如果在我的表(允许NULLS
)我得到一个错误时读取NUll
主要问题在这部分:
while( !(spRs->adoEOF) ) {
printf("%sn",
(char*) RsItem( spRs, 0L )
);
spRs->MoveNext();
}
- 还有一个问题,为什么程序在读取NULL时有错误?
我确信有一些方法可以检查对Recordset对象的空值,但我不知道应该如何在c++中完成。但是我知道如何在查询中解决这个问题。
将查询改为:
SELECT coalesce(att0, -1) as att0 FROM [dbo].[mytable]
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- c++ 结构指针在初始化为 NULL 时无法读取内存
- 在读取 Visual C++ 中的二进制文件(而不是其他编译器)后,每隔一个字符都是 NULL
- C++访问读取位置时发生冲突,类此指针为NULL
- 在 Matlab .mat 文件中读取:变量的秩和变暗正确,但数据为 NULL
- Delphi - 从 dll 调用转换(读取)C++ NULL 终止数组
- 同时使用POSIX从SERIAL读取null
- c++中的.get()在读取文件时是否返回null ?
- 从双指针数组中读取- NULL
- c++程序在读取NULL引用时崩溃
- 如何在ADO中读取NULL值
- 从 arduino 读取到 c++ 程序时继续接收 NULL