MSSQL for ADO
먼저 MSADO15.LIB 가 링크 되어 있어야 합니다. msao15.tlh msado15.tli 파일이 필요 합니다.
헤더 파일!!
#include <windows.h> #include <stdio.h> #include <comdef.h> #include <atlbase.h> #include "msado15.tlh"
class CDatabase { public: HRESULT Connect(const char *ConnectString); HRESULT ExecuteNoRecords(const char *SqlQuery); _RecordsetPtr ExecuteRecords(const char *SqlQuery); _RecordsetPtr GetDisconnectedRecords(const char *SqlQuery); CDatabase(); virtual ~CDatabase();
private: char m_ConnectString[MAX_PATH]; _CommandPtr m_Command; _RecordsetPtr m_Recordset; _ConnectionPtr m_Connection;
};
#include "Database.h"
CDatabase::CDatabase() { try { // Initialize COM CoInitialize(NULL); // Initialize ADO m_Command.CreateInstance(__uuidof(Command)); m_Recordset.CreateInstance(__uuidof(Recordset)); m_Connection.CreateInstance(__uuidof(Connection)); } catch(_com_error &e) { printf("Description = %s\n", (char*) e.Description()); } catch(...) { } }
CDatabase::~CDatabase() { try { if (m_Connection) { if(m_Connection->State == adStateOpen) { m_Connection->Close(); m_Connection.Release(); } } if (m_Recordset) { if(m_Recordset->State == adStateOpen) { m_Recordset->Close(); m_Recordset.Release(); } }
m_Connection = NULL; m_Command = NULL; m_Recordset = NULL;
CoUninitialize(); } catch(_com_error &e) { printf("Description = %s\n", (char*) e.Description()); } catch(...) { } }
HRESULT CDatabase::Connect(const char *ConnectString) { // Attemp a connect, reconnect, or leave if already connected if (strlen(ConnectString) > 0) { try { if (m_Connection == NULL) m_Connection.CreateInstance(__uuidof(Connection)); if (0 == _stricmp(ConnectString, m_ConnectString) && m_Connection->State == adStateOpen) return S_OK; else { if (m_Connection->State == adStateOpen) m_Connection->Close(); sprintf(m_ConnectString, ConnectString); } // Perform the connect if (S_OK == m_Connection->Open((char*)m_ConnectString, L"", L"", adModeUnknown)) return S_OK; } catch(_com_error &e) { printf("Description = %s\n", (char*) e.Description()); } catch(...) { } } return E_FAIL; }
_RecordsetPtr CDatabase::GetDisconnectedRecords(const char *SqlQuery) { // Attempt get a disconnected RS and terminate the connection to the DB if (strlen(SqlQuery) > 0) { try { if (m_Connection == NULL || m_Connection->State == adStateClosed) Connect(m_ConnectString);
if (m_Recordset) { if (m_Recordset->State == adStateOpen) m_Recordset->Close(); } else { m_Recordset.CreateInstance(__uuidof(Recordset)); }
m_Recordset->CursorLocation = adUseClient; m_Recordset->Open(SqlQuery, m_Connection.GetInterfacePtr(), adOpenForwardOnly, adLockBatchOptimistic, -1);
m_Recordset->PutRefActiveConnection(NULL); if(m_Connection->GetState() == adStateOpen) m_Connection->Close(); //m_Connection.Release(); //m_Connection = NULL; return m_Recordset.Detach(); } catch(_com_error &e) { printf("Description12 = %s\n", (char*) e.Description()); } catch(...) { } } return NULL; }
HRESULT CDatabase::ExecuteNoRecords(const char *SqlQuery) { if (strlen(SqlQuery) > 0) { try { if (m_Connection == NULL || m_Connection->State == adStateClosed) Connect(m_ConnectString);
m_Connection->Execute(SqlQuery, NULL, adExecuteNoRecords ); return S_OK; } catch(_com_error &e) { printf("Description = %s\n", (char*) e.Description()); } catch(...) { } } return E_FAIL; }
_RecordsetPtr CDatabase::ExecuteRecords(const char *SqlQuery) { // Attempt to execute a SQL statement, return the RS, and leave connection open if (strlen(SqlQuery) > 0) { try { if (m_Connection == NULL || m_Connection->State == adStateClosed) Connect(m_ConnectString); if (m_Recordset) { if (m_Recordset->State == adStateOpen) m_Recordset->Close(); } else { m_Recordset.CreateInstance(__uuidof(Recordset)); } m_Command->ActiveConnection = m_Connection; m_Command->CommandType = adCmdText; m_Command->CommandText = SqlQuery; _variant_t vNull; vNull.vt = VT_ERROR; vNull.scode = DISP_E_PARAMNOTFOUND; m_Recordset = m_Command->Execute( &vNull, &vNull, adCmdText ); return m_Recordset.Detach(); } catch(_com_error &e) { printf("Description = %s\n", (char*) e.Description()); } catch(...) { } }
return NULL; }
와 같이 기본적인 클래스에 대해서 만들고 선언을 합니다.
int DB_Init() { char Str[256]; sprintf(Str, "SQL OLE DB 서버 =%s;DSN= DsnName ;Initial =%s;UserI D=%s;PassWord=%s", IP, IC, ID, Password); return rad.Connect(Str); }
와 같이 초기화 하면 됩니다. 나머지 저장이나 업데이트 역시 디비 초기화와 동일한 방법을 사용하면 되겠죠? 아래와 같은 방법이면 끝나겠죠.
chat Str[256]; sprintf(Str,"update 테이블 set 저장하고자하는...", 대상들 );
원본 : http://ngps.net/ngpiki/index.php?display=MSSQL%20for%20ADO |