#include "stdafx.h" #include "stdio.h"
#include "Windows.h"
#include "string.h"
#import "C:/Program Files/Common Files/System/ado/msado20.tlb" no_namespace rename("EOF", "adoEOF")
HRESULT ComInit();
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
_ConnectionPtr pCn;
_RecordsetPtr pRs;
_variant_t vtEmpty;
_variant_t vRsinfo;
long ccc;
char jj[255];
int j;
ComInit();
pCn.CreateInstance(__uuidof(Connection));
pRs.CreateInstance(__uuidof(Recordset));
pCn->Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C://VcADO//users.mdb","","",-1);
if(pCn->State==0){
MessageBox(0,"連接數據庫失敗。","連接失敗",MB_OK);
}else{
vtEmpty="select * from 用戶信息表";
pRs->Open(vtEmpty, pCn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
ccc=pRs->RecordCount;
if(ccc>=1){
vRsinfo=pRs->FIElds->GetItem("姓名")->GetValue();
j=sprintf(jj,"查詢到:%s",(char*)(_bstr_t)vRsinfo);
MessageBox(0,jj,"查詢成功",MB_OK);
pRs->Close();
}else{
MessageBox(0,"沒有查詢到數據.","查詢失敗",MB_OK);
}
pCn->Close();
}
return 0;
}
//Examda提示: COM 使用時初始化
HRESULT ComInit()
{
HRESULT hr = S_OK; // 默認返回值
if FAILED(CoInitialize(NULL)) // COM 初始化調用
{
CoUninitialize();
hr = E_UNEXPECTED;
}
return hr;
}