stdafx.h
-----------------------
// stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 // #pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #include <fstream> #include <iostream> #include <ole2.h> //#include <stdio.h> #include <conio.h> using namespace std; // TODO: 在此处引用程序需要的其他头文件 //c++中引用ADO库的方法 //ADO库的引用 //导入ADO动态链接库,必须的 #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
//------------------------------
#pragma once //#include "ReadAdoValue.h" //C++中使用ADO库组件一例 #include "stdafx.h" //定义函数 void pdbvalue2(const char *filepath); void LoadCOM(void); void UnLoadCOM(void); class ReadAdoValue { public: ReadAdoValue(void); ~ReadAdoValue(void); void pdbvalue(void); };
ReadAdoValue.cpp
//-------------------------
#include "StdAfx.h" #include "ReadAdoValue.h" ReadAdoValue::ReadAdoValue(void) { } ReadAdoValue::~ReadAdoValue(void) { } void ReadAdoValue::pdbvalue(void) { pdbvalue2("d:\\test.mdb"); } void LoadCOM(void) { CoInitialize(NULL); //初始化COM,必须的 } void UnLoadCOM(void) { CoUninitialize(); //释放COM } //读取数据库表内容的方法 void pdbvalue2(const char *filepath) { CoInitialize(NULL); //初始化COM,必须的 //LoadCOM(); _ConnectionPtr m_pConnection; //创建智能指针,必须的 //m_pConnection.CreateInstance(__uuidof(Connection)); //或 m_pConnection.CreateInstance("ADODB.Connection"); try { //连接字符串,Student为数据库名 _bstr_t constr; //mdb数据库连接字符串 _bstr_t mdbpath="d:\\test.mdb"; constr="Provider=Microsoft.Jet.OLEDB.4.0;"; constr += "Data Source=" + mdbpath + ";"; constr += "user id=Admin;"; constr += "password=;"; constr += "Persist Security Info=True;"; //SQL Server数据库连接字符串 //constr="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Student;Data Source=hsg\\hsg"; //Oracle数据库连接字符串 //打开连接方法1 m_pConnection->ConnectionString=constr; cout<<constr<<endl; //调用Open方法 //m_pConnection->Open("","","",adModeUnknown); //1 //m_pConnection->Open(constr,"","",adModeUnknown); //2 m_pConnection->Open(constr,"","",adConnectUnspecified); cout<<"数据库初始化成功!"<<endl; } catch(_com_error &e) //捕获连接异常 { cout<<"数据库初始化错误! "<<endl; const TCHAR *msg; msg=e.ErrorMessage();//_T("Error Description="); cout<<"ErrorMessage="<<msg<<endl; // return; } cout<<"连接成功!"<<endl; _RecordsetPtr pRst(__uuidof(Recordset)); pRst=m_pConnection->Execute("select * from student",NULL,adCmdText);//使用Execute方法,student为表名 if(!pRst->adoEOF) { pRst->MoveFirst(); } else { cout<<"表内数据为空"<<endl; return; } // 读入库中各字段并加入列表框中 _variant_t var; string strName; //CString strName; //LPCTSTR strName; char* line; while(!pRst->adoEOF) { var = pRst->GetCollect("STDName"); //var =pRst->GetFields()->GetItem("STDName")->GetValue(); if(var.vt != VT_NULL) { strName = (LPCSTR)_bstr_t(var); //strName =(char *)_bstr_t(var); } line=(char *)strName.c_str(); //str.c_str() cout<<line<<endl; pRst->MoveNext(); } try { //关闭 pRst->Close(); m_pConnection->Close(); //Release pRst.Release(); m_pConnection->Release(); } catch(_com_error &r) { } CoUninitialize(); //释放COM //UnLoadCOM(); }