// EGDatabase.h #pragma once /* 在VC++2005编译.cpp文件中使用using namespace std;后编译提示: “std”: 具有该名称的命名空间不存在是未包含iostream文件导致的!*/ #include <iostream> #include "stdio.h" #include <string.h> #include <assert.h> using namespace std; #import "C:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename ("EOF", "adoEOF") class EGSQLStatement{ public: EGSQLStatement() { } // index从1开始算 inline void SetString(int index, string str) { index--; assert(index>=0 && index<MAX_PLACE_HOLDER); placeHolder[index].type = EG_STRING; placeHolder[index].value.stringData = str; } // index从1开始算 inline void SetInt(int index, int interger) { index--; assert(index>=0 && index<MAX_PLACE_HOLDER); placeHolder[index].type = EG_INT; placeHolder[index].value.intData = interger; } inline EGSQLStatement& operator = (string str) { sqlString = str; return *this; } inline string GetStdString() { return sqlString; } string GetStdSQL(); private: const static int MAX_PLACE_HOLDER = 64; enum FieldType { EG_INT, EG_STRING, }; struct FieldValue { int intData; string stringData; }; struct Placeholder { FieldType type; FieldValue value; }; Placeholder placeHolder[MAX_PLACE_HOLDER]; // 最多可使用64个占位符 string sqlString; }; class EGResultSet { public: int GetFieldIntByName(string fieldName) { _variant_t result = pRecordset->GetCollect((_bstr_t)fieldName.c_str()); return (int)result.intVal; } string GetFieldStringByName(string fieldName) { _variant_t result = pRecordset->GetCollect((_bstr_t)fieldName.c_str()); return (const char*)_bstr_t(result); } bool GetFieldBoolByName(string fieldName) { _variant_t result = pRecordset->GetCollect((_bstr_t)fieldName.c_str()); return (bool)result.boolVal; } long GetFieldLongByName(string fieldName) { _variant_t result = pRecordset->GetCollect((_bstr_t)fieldName.c_str()); return (long)result.lVal; } float GetFieldFloatByName(string fieldName) { _variant_t result = pRecordset->GetCollect((_bstr_t)fieldName.c_str()); return (float)result.fltVal; } double GetFieldDoubleByName(string fieldName) { _variant_t result = pRecordset->GetCollect((_bstr_t)fieldName.c_str()); return (double)result.dblVal; } int GetFieldIntByIndex(int index) { _variant_t result = pRecordset->GetCollect(index); return (int)result.intVal; } string GetFieldStringByIndex(int index) { _variant_t result = pRecordset->GetCollect(index); return (const char*)_bstr_t(result); } bool GetFieldBoolByIndex(int index) { _variant_t result = pRecordset->GetCollect(index); return (bool)result.boolVal; } long GetFieldLongByIndex(int index) { _variant_t result = pRecordset->GetCollect(index); return (long)result.lVal; } float GetFieldFloatByIndex(int index) { _variant_t result = pRecordset->GetCollect(index); return (float)result.fltVal; } double GetFieldDoubleByIndex(int index) { _variant_t result = pRecordset->GetCollect(index); return (double)result.dblVal; } long MoveNext() { return pRecordset->MoveNext(); } long MoveFirst() { return pRecordset->MoveFirst(); } long MoveLast() { return pRecordset->MoveLast(); } long MovePrevious() { return pRecordset->MovePrevious(); } long Close() { return pRecordset->Close(); } public: _RecordsetPtr pRecordset; }; class EGDatabase { public: EGDatabase() { ::CoInitialize(NULL); bConnect = false; } ~EGDatabase() { ::CoUninitialize(); } inline bool IsConnected(){ return bConnect; } bool Connection(string databaseName, string user, string password, bool error = false, string driverName = "myodbc", string serverIP = "localhost"); bool Execute(string sql, bool error = false); bool Execute(EGSQLStatement sql, bool error = false); EGResultSet Query(string sql, bool error = false); EGResultSet Query(EGSQLStatement sql, bool error = false); bool Disconnect(bool error = false); private: _ConnectionPtr pConnection; bool bConnect; }; #include "EGDatabase.h" #include <tchar.h> string EGSQLStatement::GetStdSQL() { // 将占位符替换成具体的参数 string stdSQL = sqlString; string newString; char buffer[7]; size_t loc = -1; int i = 0; while (true) { loc = stdSQL.find("?", loc+1); if (string::npos == loc) { break; } else { newString = stdSQL.substr(0, loc); newString += "/'"; // 将该问号替换成具体的参数 switch(placeHolder[i].type) { case EG_INT: { // 将int型的参数转换为char* itoa(placeHolder[i].value.intData, buffer, 10); newString += buffer; break; } case EG_STRING: { newString += placeHolder[i].value.stringData; break; } default:; } newString += "/'"; newString += stdSQL.substr(loc+1, sqlString.length()); stdSQL = newString; i++; } } return stdSQL; } bool EGDatabase::Connection(string databaseName, string user, string password, bool error /* = false */, string driverName /* = "myodbc" */, string serverIP/* = "localhost" */) { if (true == bConnect) { return true; } try { string connectURL = ""; connectURL += "DSN="; connectURL += driverName; connectURL += ";Server="; connectURL += serverIP; connectURL += ";Database="; connectURL += databaseName; // 注意string转_bstr_t的方法 // (_bstr_t)string.c_str() pConnection.CreateInstance("ADODB.Connection"); pConnection->Open((_bstr_t)connectURL.c_str(), (_bstr_t)user.c_str(), (_bstr_t)password.c_str(), adModeUnknown); bConnect = true; return true; } catch (_com_error e) { char *errorMsg; errorMsg = _com_util::ConvertBSTRToString(e.Description()); if (true == error) { printf("%s/n", errorMsg); } return false; } } EGResultSet EGDatabase::Query(string sql, bool error /* = false */) { _bstr_t bstrSQL = (_bstr_t)sql.c_str(); EGResultSet rs; try { rs.pRecordset.CreateInstance(__uuidof(Recordset)); rs.pRecordset->Open(bstrSQL, pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); return rs; } catch (_com_error e) { char *errorMsg; errorMsg = _com_util::ConvertBSTRToString(e.Description()); if (true == error) { printf("%s/n", errorMsg); } return rs; } return rs; } EGResultSet EGDatabase::Query(EGSQLStatement sql, bool error /* = false */) { return Query(sql.GetStdString(),error); } bool EGDatabase::Execute(string sql, bool error /* = false */) { _bstr_t bstrSQL = (_bstr_t)sql.c_str(); try { pConnection->Execute(bstrSQL, NULL, adExecuteNoRecords); return true; } catch (_com_error e) { char *errorMsg; errorMsg = _com_util::ConvertBSTRToString(e.Description()); if (true == error) { printf("%s/n", errorMsg); } return false; } } bool EGDatabase::Execute(EGSQLStatement sql, bool error /* = false */) { return Execute(sql.GetStdString(),error); } bool EGDatabase::Disconnect(bool error /* = false */) { try { pConnection->Close(); pConnection->Release(); bConnect = false; return true; } catch (_com_error e) { char *errorMsg; errorMsg = _com_util::ConvertBSTRToString(e.Description()); if (true == error) { printf("%s/n", errorMsg); } return false; } }