C++ 操作数据库类

#pragma once
#include <string>
#include <windows.h>
#include <algorithm>
#include <vector>
#include <comdef.h>
#import "c:\\program files\\common files\\system\\ado\\msado15.dll"  no_namespace rename("EOF", "adoEOF")
#pragma warning(disable:4996)
using namespace std;
class SQLOperator
{
public:
	SQLOperator();
	~SQLOperator();
	bool InitSQL(const string& strIP, const string& strUser, const string& strPwd, const string& strDBName);
	BOOL GetSqlInfo(const string& strInfo, int nColumn, vector<vector<string>>&vInfo);
	BOOL DoSql(const string& strInfo);
	void CloseSql();

private:
	_ConnectionPtr pConnection;
	_CommandPtr pCmd;
	_RecordsetPtr pRecord;
};

.cpp文件:<pre name="code" class="cpp">#include "stdafx.h"
#include "SQL.h"
#include <Winerror.h>
SQLOperator::SQLOperator()
{

}
bool SQLOperator::InitSQL(const string& strIP, const string& strUser, const string& strPwd, const string& strDBName)
{
	::CoInitialize(NULL);
	try{
		HRESULT hr = pConnection.CreateInstance("ADODB.Connection");//
		if (SUCCEEDED(hr)){
			char tcData[MAX_PATH * 4] = { 0 };
			sprintf(tcData, "Driver={sql server};server=%s,1433;uid=%s;pwd=%s;database=%s;",
				strIP.c_str(), strUser.c_str(), strPwd.c_str(), strDBName.c_str());
			hr = pConnection->Open(tcData, "", "", adModeUnknown);
			if (SUCCEEDED(hr)){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}
	catch (_com_error e){
		::MessageBox(NULL, e.Description(), _T("异常"), MB_OK);
		return false;
	}
	return true;
}
BOOL SQLOperator::GetSqlInfo(const string& strInfo, int nColumn, vector<vector<string>>&vInfo)
{
	try{
		_variant_t vAffected;
		pRecord = pConnection->Execute(strInfo.c_str(), &vAffected, adCmdText);

		//枚举查询结果集
		if (!pRecord->adoEOF){
			pRecord->MoveFirst();
		}
		else{
			return FALSE;
		}
		while (!pRecord->adoEOF){
			VARIANT vtID[16];
			_variant_t vt[16];
			string str[16];
			vector<string>vTemp;
			for(int i = 0; i < nColumn; i++){
				vtID[i].vt = VT_I4;
				vtID[i].lVal = i;
				vt[i] = (pRecord->Fields->GetItem(vtID[i])->Value);
			}

			for (int i = 0; i < nColumn; i++){
				if (vt[i].vt == VT_NULL){
					str[i] = " ";
				}
				else{
					str[i] = (char*)(_bstr_t)vt[i];
				}
				vTemp.push_back(str[i]);
			}
			vInfo.push_back(vTemp);
			pRecord->MoveNext();
		}
	}
	catch (_com_error e){
		::MessageBox(NULL, e.Description(), _T("异常"), MB_OK);
		return false;
	}
	return true;
}
BOOL SQLOperator::DoSql(const string& strInfo)
{
	try{
		_variant_t vAffected;
		pConnection->Execute(strInfo.c_str(), &vAffected, adCmdText);
		return vAffected.intVal;
	}
	catch (_com_error e){
		::MessageBox(NULL, e.Description(), _T("异常"), MB_OK);
	}
	return false;
}
void SQLOperator::CloseSql()
{
	//pRecord->Close();
	pConnection->Close();
	::CoUninitialize();
}
SQLOperator::~SQLOperator()
{

}

 


main函数:
<pre name="code" class="cpp">int _tmain(int argc, _TCHAR* argv[])
{
     SQLOperator sql;//实例化类对象
     sql.InitSQL("192.168.1.87", "", "", "IMSDB1");//初始化SQL语句
     sql.GetSqlInfo("select a.CardNo, b.LimitBegin, b.LimitEnd from HrEmployee a left join LtSiteGrant b \
on a.EmpID = b.EmpID where b.SiteID = 1 and b.GrantState = 1");
//sql.DoSql("delete from chd_info");
     sql.CloseSql();//调用类成员函数
     return 0;
}




 

posted @ 2015-05-03 08:52  QQ76211822  阅读(1593)  评论(0编辑  收藏  举报