请问使用VC开发ADO数据库程序时,如何获得字段类型为整型或浮点型的数据?

d5 2000-08-28 07:02:00
_variant_t及_bstr_t好像只能用于字符型
...全文
354 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
seesi 2001-05-31
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/read_article.asp?id=7607我的文章
netskiff 2001-05-25
  • 打赏
  • 举报
回复
关注!
halbert 2000-11-22
  • 打赏
  • 举报
回复
关于vc++ 的ado 编程
atdafx.h
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")

BOOL CGetemailApp::InitInstance()
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("ole 出错");
return FALSE;
}
::CoInitialize( NULL );
}
int CGetemailApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
return CWinApp::ExitInstance();
}

bool getrecordset()
{
char SqlStr[255],account[50];
char Error[500];
char putfilestr[300];
long recordnum;
m_pConnection = NULL;
m_pRecordset = NULL;
_variant_t TheEmail,ThePhone_office,TheAccount,TheUser_id;
char strEmail[50],strPhone_offiece[50],strAccount[50],strUser_id[50];
if(!OpenFileForReadWrite())
return ;
try{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
recordnum=m_pConnection->Open("DSN=stat","test","test",-1);
sprintf(putfilestr,"%20s %30s %40s %30s\n","用户编码","用户帐号","用户Email","用户办公电话");
fwrite(putfilestr,strlen(putfilestr),1,fpout);
while(!feof(fpin))
{
fscanf(fpin,"%s",account);
//sprintf(SqlStr,"select a.user_id,a.account, b.email,b.phone_office from userbasicinfo a,usercontactinfo b where a.account='%s' and b.user_id=a.user_id",account);
strcpy(SqlStr,"select * from region");
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
//用 recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
// 也出错
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
if(!m_pRecordset->adoEOF)
{
TheUser_id=m_pRecordset->GetCollect("user_id");

TheAccount=m_pRecordset->GetCollect("account");
TheEmail=m_pRecordset->GetCollect("email");
ThePhone_office=m_pRecordset->GetCollect("phone_office");
sprintf(putfilestr,"%20s %30s %40s %30s\n",TheUser_id.bstrVal,TheAccount.bstrVal,TheEmail.bstrVal,ThePhone_office.bstrVal);
fwrite(putfilestr,strlen(putfilestr),1,fpout);
}//end if
m_pRecordset->Close();
}//end while
}//end try
catch(_com_error *e)
{

//CString Error = e->ErrorMessage();
//char Error[500];
strcpy(Error , (char*)e->Description());
AfxMessageBox(e->ErrorMessage());
}
catch(_com_error e)
{

//CString Error = e->ErrorMessage();
strcpy(Error , (char*)e.Description());
//AfxMessageBox(e->ErrorMessage());
AfxMessageBox(Error);
}
catch(...)
{
AfxMessageBox("ado 出错");
}

m_pRecordset->Close();;
m_pConnection->Close();
m_pConnection->Release();
CloseAllFile();
}
// 执行到recordnum=m_pRecordset->adoEOF; 出错 为何
// 请那位大侠指点

maptrix 2000-09-05
  • 打赏
  • 举报
回复
Ray_czh是完全正确的。
通过判断var.vt的值可以判断是什么类型
VT_I2:是短整型,通过V_I2(&var)可以获得其值。
VT_I4:是长整型,通过V_I4(&var)可以获得其值。
VT_R4:是浮点型,通过V_R4(&var)可以获得其值。
VT_R8:是双精度型,通过V_R8(&var)可以获得其值。
--茶-- 2000-08-29
  • 打赏
  • 举报
回复
_variant_t tamount;
tamount=pRsMdb->GetCollect("装配数量");
double dblNum;
if(tamount.vt!=VT_NULL)
{
tamount.ChangeType(VT_R8);
dblNum=tamount.dblVal;
}
//如果想转换为整型则:
tamount.ChangeType(VT_I4);
dblNum=tamount.lVal;
ufc 2000-08-29
  • 打赏
  • 举报
回复
用类CDBVariant 也一样
CDBVariant var;
m_dataset.GetFieldValue((int)0,Var);
再对Var.m_dwType类型作数据类型判断;然后取相应的值
var.pstring--->字符串var.lval----->整数.....



Ray_czh 2000-08-29
  • 打赏
  • 举报
回复
_variant_t 是COM中的数据类型,实际上是个集合类,在ADO中可以通过判断_variant_t中的VT的类型,来判断出他的类型比如
_variant_t var;
CString str;
switch(var.vt)
{
case:VT_I2:strRet.Format(_T("%hd"),V_I2(&var));break;
case:VT_I4:strRet.Format(_T("%d"),V_I4(&var));break;
case:VT_R4:strRet.Format(_T("%f"),V_R4(&var));break;
case:VT_R8:strRet.Format(_T("%f"),V_R8(&var));break;
default:break;
}

U皮特U 2000-08-28
  • 打赏
  • 举报
回复
读出来转换成整型或浮点型不就行了?用atoi或atof函数

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧