1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| #include "StdAfx.h" #include "ADOConnect.h"
CAdoConnect::CAdoConnect(const CString& szConnectStr) :m_szConnectStr(szConnectStr) , m_szServer(_T("")) , m_szDATABASE(_T("")) , m_szUID(_T("")) , m_szPWD(_T("")) , m_nPort(1433) { ::CoInitialize(NULL); } CAdoConnect::CAdoConnect(const CString& Server , const CString& DATABASE , const CString& UID , const CString& PWD , const int nPort ) : m_szServer(Server) , m_szDATABASE(DATABASE) , m_szUID(UID) , m_szPWD(PWD) , m_szConnectStr(_T("")) , m_nPort(nPort) { m_szConnectStr.Empty(); ::CoInitialize(NULL); } CAdoConnect::~CAdoConnect(void) { if (m_pRecordset != NULL) { if (m_pRecordset->GetState() == adStateOpen) { m_pRecordset->Close(); } if (m_pRecordset) { m_pRecordset.Release(); m_pRecordset = NULL; } } if (m_pConnection != NULL) { if (m_pConnection->GetState() == adStateOpen) { m_pConnection->Close(); } m_pConnection.Release(); m_pConnection = NULL; } ::CoUninitialize(); }
BOOL CAdoConnect::OnInitAdoConnection(void) { try { HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection"); if (SUCCEEDED(hr)) { m_pConnection->CursorLocation = adUseServer; CString szStr; if (m_szConnectStr.IsEmpty()) { szStr.Format(_T("driver={SQL Server};Server=%s,%d;DATABASE=%s;UID=%s;PWD=%s;") , m_szServer, m_nPort, m_szDATABASE, m_szUID, m_szPWD); } else { szStr = m_szConnectStr; } _bstr_t strConnect = szStr; HRESULT hr = m_pConnection->Open(strConnect, "", "", adModeUnknown); if (SUCCEEDED(hr)) { return TRUE; } else { AfxMessageBox("建立数据库连接失败!"); return FALSE; } } } catch (_com_error e) { AfxMessageBox(e.Description()); return FALSE; } catch (...) { return FALSE; } return TRUE; }
BOOL CAdoConnect::ExecuteSQL(_bstr_t bstrSQL, long lOptions) { try { if (m_pConnection == NULL) { OnInitAdoConnection(); } m_pRecordset = m_pConnection->Execute(bstrSQL, NULL, lOptions); return TRUE; } catch (_com_error e) { AfxMessageBox(e.Description()); return FALSE; } catch (...) { return FALSE; } }
_RecordsetPtr& CAdoConnect::GetRecordSet(_bstr_t bstrSQL) { try { if (m_pConnection == NULL) { OnInitAdoConnection(); } HRESULT hr = m_pRecordset.CreateInstance(__uuidof(Recordset)); if (SUCCEEDED(hr)) { m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } } catch (_com_error e) { AfxMessageBox(e.Description()); } return m_pRecordset; }#pragma once
#import "c:\Program Files\Common Files\System\ado\msado15.dll"no_namespace \ rename("EOF", "adoEOF")rename("BOF", "adoBOF") class CAdoConnect { private: CString m_szConnectStr; CString m_szServer; CString m_szDATABASE; CString m_szUID; CString m_szPWD; int m_nPort; private: _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; public: CAdoConnect(const CString& szConnectStr); CAdoConnect(const CString& Server , const CString& DATABASE , const CString& UID , const CString& PWD , const int nPort = 1433 ); virtual~CAdoConnect(void); public: BOOL OnInitAdoConnection(void); BOOL ExecuteSQL(_bstr_t bstrSQL, long lOptions = adCmdText); _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL); public: _RecordsetPtr& operator->() { return m_pRecordset; } };
|