C++中使用ADO库组件一例

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")


ReadAdoValue.h

//------------------------------

#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(); }


转载于:https://www.cnblogs.com/sqlite3/archive/2011/10/28/2566799.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值