经测试,连接数据库能成功,但是从数据库表取值失败,估计是类型转换问题,代码如下:
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
pConn->Open("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=lhw;Initial Catalog=CRC16;Data Source=ACER-PC;Server=localhost","lhw","lhw",adConnectUnspecified);
_RecordsetPtr pRs("ADODB.Recordset");
pRs->Open("Select * From CRC",_variant_t(pConn,true),adOpenStatic,adLockOptimistic,adCmdText);
//////////////////////////////////////////////以上都没问题,调试发现下面报错
while (!pRs->EndOfFile)
{
r1=atoi(_bstr_t(pRs->GetCollect("POS1")));//运行到这出错,本来想用这个语句读取表中数据
r2=atoi(_bstr_t(pRs->GetCollect("POS2")));
r3=atoi(_bstr_t(pRs->GetCollect("POS3")));
pRs->AddNew();
pRs->Fields->Item["CRC"]->Value=_variant_t(long((crc16bitfor1(r1)^crc16bitfor1(r2))^crc16bitfor1(r3)));//这个语句用来添加数据
}
pRs->Close();
pConn->Close();
}
catch(_com_error&e)
{
MessageBox(e.ErrorMessage());
}
表有四列POS1,POS2,POS3,CRC,都是int类型
请教如何解决?而且不知道错在哪.....数据库SQLMFCVC
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
pConn->Open("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=lhw;Initial Catalog=CRC16;Data Source=ACER-PC;Server=localhost","lhw","lhw",adConnectUnspecified);
_RecordsetPtr pRs("ADODB.Recordset");
pRs->Open("Select * From CRC",_variant_t(pConn,true),adOpenStatic,adLockOptimistic,adCmdText);
//////////////////////////////////////////////以上都没问题,调试发现下面报错
while (!pRs->EndOfFile)
{
r1=atoi(_bstr_t(pRs->GetCollect("POS1")));//运行到这出错,本来想用这个语句读取表中数据
r2=atoi(_bstr_t(pRs->GetCollect("POS2")));
r3=atoi(_bstr_t(pRs->GetCollect("POS3")));
pRs->AddNew();
pRs->Fields->Item["CRC"]->Value=_variant_t(long((crc16bitfor1(r1)^crc16bitfor1(r2))^crc16bitfor1(r3)));//这个语句用来添加数据
}
pRs->Close();
pConn->Close();
}
catch(_com_error&e)
{
MessageBox(e.ErrorMessage());
}
表有四列POS1,POS2,POS3,CRC,都是int类型
请教如何解决?而且不知道错在哪.....数据库SQLMFCVC
解决方案 »
- 怎么判断editbox中是否存在数据?
- 编译优化的问题
- Sleep()的迷惑??
- VC字符串处理函数不会写,在线等
- 我想调用一个程序,最大化启动,ShellExecute(this->m_pMainWnd->m_hWnd, "open", szJarFile, NULL, NULL,SW_MAXIMIZE );可是还是最小化启
- 关于动态链接库接口的参数与POWERBUILDER数据类型匹配问题!
- 一个关于钩子的问题!请高手指点!谢谢!!!
- 我擦勒!谁能告诉我这是啥灵异现象?
- F8
- 关于在mfc中使用msxml读取xml文件的疑问。xml文件的格式和源码如下。没有报错,但无法将xml数据读入。
- listbox 添加行怎么没显示
- 如何用程序唤醒睡眠中的电脑
这是atoi的原型int atoi( const char *string );
_bstr_t是什么类型你用这个函数?
这是atoi的原型int atoi( const char *string );
_bstr_t是什么类型你用这个函数?int强制转换不行 error C2440: 'type cast' : cannot convert from 'class _bstr_t' to 'int' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
这是atoi的原型int atoi( const char *string );
_bstr_t是什么类型你用这个函数?无法从'class _bstr_t' 转化到int,如何解决呢?
以前用这个语句都行:
MessageBox(_bstr_t(pRs->GetCollect("ID")));//ID也是整形的,能输出出来,但是现在改成上面后就不行了。
这是atoi的原型int atoi( const char *string );
_bstr_t是什么类型你用这个函数?无法从'class _bstr_t' 转化到int,如何解决呢?
以前用这个语句都行:
MessageBox(_bstr_t(pRs->GetCollect("ID")));//ID也是整形的,能输出出来,但是现在改成上面后就不行了。这样写:
_variant_t Holder;//保存记录集某项数据
Holder = pRs->GetCollect("ID")));
int Id = (int)Holder;
试下
int id = var.intVal;
这是atoi的原型int atoi( const char *string );
_bstr_t是什么类型你用这个函数?无法从'class _bstr_t' 转化到int,如何解决呢?
以前用这个语句都行:
MessageBox(_bstr_t(pRs->GetCollect("ID")));//ID也是整形的,能输出出来,但是现在改成上面后就不行了。这样写:
_variant_t Holder;//保存记录集某项数据
Holder = pRs->GetCollect("ID")));
int Id = (int)Holder;
试下
试了您的方法,依然报错,CATCH出来的是:调试的时候在_variant_t var1 = pRs->GetCollect("POS1");处捕捉到错误。
试了您的方法,依然报错,CATCH出来的是:调试的时候在_variant_t var1 = pRs->GetCollect("POS1");处捕捉到错误。
同意,这样就对了试了上面的方法,依然报错,CATCH出来的是:调试的时候在_variant_t var1 = pRs->GetCollect("POS1");处捕捉到错误。
同意,这样就对了试了上面的方法,依然报错,CATCH出来的是:调试的时候在_variant_t var1 = pRs->GetCollect("POS1");处捕捉到错误。如果是在:_variant_t var1 = pRs->GetCollect("POS1");处捕捉到错误。可能不是格式转换的错误了,是不是 上面打开数据库或者运行sql语句有问题?可以将上面两条语句分别放到try catch中,看看有没有报异常,或者open语句后,智能指针是空的,才导致下面取不到数的?
同意,这样就对了试了上面的方法,依然报错,CATCH出来的是:调试的时候在_variant_t var1 = pRs->GetCollect("POS1");处捕捉到错误。如果是在:_variant_t var1 = pRs->GetCollect("POS1");处捕捉到错误。可能不是格式转换的错误了,是不是 上面打开数据库或者运行sql语句有问题?可以将上面两条语句分别放到try catch中,看看有没有报异常,或者open语句后,智能指针是空的,才导致下面取不到数的?
所有与SQL连接有关的语句都在try中,调试的时候Prs与pConn指针都不是空的
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")string str = ConvertBSTRToString(_bstr_t(pRs->GetCollect("POS1")));
int iDes = atoi(str);
谢谢回复,但是提示我error C2065: 'ConvertBSTRToString' : undeclared identifier
确认已经添加了头文件和引用参数,为什么还报错?
改成while(!pRs->adoEOF)