日期:2014-05-16 浏览次数:20544 次
#include "stdafx.h"
#include <stdio.h>
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
int main(int argc, char* argv[])
{
char term[]="KN 04";
_ConnectionPtr m_pConnection; //到access数据库的链接对象
_RecordsetPtr m_pRecordset; //结果集对象
CoInitialize(NULL); //初始化
m_pConnection.CreateInstance(__uuidof(Connection)); //实例化对象
//连到具体某个mdb
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=supp_desc.mdb", "", "", adModeUnknown);
}
catch(_com_error e)
{
printf("数据库连接失败!");
return 0;
}
m_pRecordset.CreateInstance(__uuidof(Recordset)); //实例化结果集对象
//执行sql语句
try
{
char sql[300];
memset(sql,0,300);
strcat(sql,"SELECT descr FROM sd WHERE supp=\'");
strcat(sql,term);
strcat(sql,"\'");
m_pRecordset->Open(sql, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch(_com_error *e)
{
printf(e->ErrorMessage());
if(m_pConnection->State)
{
m_pConnection->Close();
m_pConnection= NULL;
}
return 0;
}
//处理结果集
try
{
//若结果为空,结束
if(m_pRecordset->BOF)
{
printf("表内数据为空!");
if(m_pConnection->State)
{
m_pRecordset->Close();
m_pRecordset = NULL;
m_pConnection->Close();
m_pConnection= NULL;
}
return 0;
}
//游标定位到第一条记录
m_pRecordset->MoveFirst();
_variant_t var; //从结果集中取出的数据放到var中
char *t1;
while(!m_pRecordset->adoEOF)
{
var= m_pRecordset->GetCollect("descr");
if(var.vt != VT_NULL)
{
t1 = _com_util::ConvertBSTRToString((_bstr_t)var);
}
printf(t1);
printf("\n");
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
printf(e->ErrorMessage());
}
//退出程序时的处理
if(m_pConnection->State)
{
m_pRecordset->Close();
m_pRecordset = NULL;
m_pConnection->Close();
m_pConnection= NULL;
}
return 0;
}