第1个回答 2013-06-18
下面是我写的一个数据库类你看下吧! 头文件**********************************************************************#ifndef DATABASE_CLASS_H
#define DATABASE_CLASS_H//引用COM组件DLL
#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "adoEOF")class DataBase
{
public:
//自定义设置连接字符串,可随意选择连接类型
DataBase(std::string strConn = "");
~DataBase();
//连接Access数据库,传递数据库路径,或无参,通过构造函数设置连接字符串连接数据库
int Open(std::string strAccessPath = "");
//设置Sql server数据库信息,并连接
int Open(std::string strHost, std::string strDatabase, std::string strUsername, std::string strPassword);
//判断指向数据是否存在
bool Empty(void);
//传递列名,获取该行该列数据
_variant_t operator[](const std::string &strIndex);
//移动指向数据
void Move(int i);
//转换为std::string数据类型
const std::string toStr(const _variant_t &var);
//设置数据命令
friend DataBase& operator<<(DataBase& data, std::string strCmd);
//执行命令,index为影响行数
friend DataBase& operator>>(DataBase& data, int &index);public:
typedef _RecordsetPtr DataSet;
//数据移动
enum MoveType{First = 0, Next = 1, Last = 2};private:
std::string commString; //数据库命令
std::string connString; //数据库连接字符串
_ConnectionPtr connPtr; //数据库连接对象
DataSet dataSet; //数据集合
};#endif 源文件**************************************************************************#include <windows.h>
#include <iostream>
#include <string>
#include "DataBase.h"DataBase::DataBase(std::string strConn):connString(strConn)
{
CoInitialize(NULL); //初始化COM库 connPtr.CreateInstance(__uuidof(Connection));
dataSet.CreateInstance(__uuidof(Recordset));
}DataBase::~DataBase()
{
//如果该对象被打开我就关闭它
if (connPtr->State == adStateOpen)
{
connPtr->Close();
}
if (dataSet->State == adStateOpen)
{
dataSet->Close();
}
//在构造函数中创建了该对象,因此要销毁它
connPtr.Release();
dataSet.Release();
//卸载COM库
CoUninitialize();
}int DataBase::Open(std::string strAccessPath)
{
if (!strAccessPath.empty())
connString = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" + strAccessPath;
try
{
//设置数据库连接字符串并连接
connPtr->ConnectionString = connString.c_str();
connPtr->Open("", "", "", -1);
}
catch(_com_error e)
{
return -1;
}
return 0;
}int DataBase::Open(std::string strHost, std::string strDatabase, std::string strUsername, std::string strPassword)
{
try
{
//设置连接字符串并连接
connString = "Driver={SQL Server};Server=" + strHost + ";Database=" + strDatabase + ";Uid=" + strUsername + ";Pwd=" + strPassword + ";";
connPtr->ConnectionString = connString.c_str();
connPtr->Open("", "", "", -1);
}
catch(_com_error e)
{
std::cout << e.Description();
return -1;
}
return 0;
}DataBase& operator<<(DataBase& data, std::string strCmd)
{
//设置SQL语句
data.commString = strCmd; return data;
}DataBase& operator>>(DataBase& data,int &index)
{
try
{
//执行SQL语句
data.dataSet = data.connPtr->Execute(data.commString.c_str(), NULL, -1);
index = data.dataSet;
}
catch(_com_error e)
{
index = -1;
} return data;
}bool DataBase::Empty(void)
{
if (connPtr->State != adStateOpen)
return false;
return dataSet->adoEOF;
}_variant_t DataBase::operator[](const std::string &strIndex)
{
_variant_t varData;
try
{
//获取当前指向行的该列数据
varData = dataSet->GetCollect(strIndex.c_str());
}
catch(_com_error e)
{
return NULL;
}
return varData;
}void DataBase::Move(int index)
{
switch(index)
{
case DataBase::First:
dataSet->MoveFirst(); //移动到一开始
break;
case DataBase::Next:
dataSet->MoveNext(); //移动到下一行
break;
case DataBase::Last:
dataSet->MoveLast(); //移动到最后的下一个
break;
}
}const std::string DataBase::toStr(const _variant_t &var)
{
const std::string str = (const char*)(_bstr_t)var;
return str;
}