• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    ADODB Connector
    Last Modify : 27 December, 2010(11:20)
    /***************************************************************************
     *
     *		Date			: 2006-06-08
     *		Copyright		: Kurome
     *		E-mail			: aucd29@gmail.com
     *		URL			: http://www.sarangnamu.net
     *		
     *
     ***************************************************************************/
    
    
    #pragma once
    #pragma warning(push)                         //ado 버그로 인한 컴파일시 경고 뜨는것 방지.
    #pragma warning(disable:4146)
    #import "C:\Program Files\Common Files\System\ADO\msado15.dll" rename("EOF", "EndOfFile")
    #pragma warning(pop)
    using namespace ADODB;
    
    class CMyAdoConn
    {
    public:
    	CMyAdoConn(void);
    public:
    	~CMyAdoConn(void);
    private:
    	CString m_szDBFile;
    public:
    	void SetDBFile(CString szDBFile);
    	void DBConn(void);
    
    	_ConnectionPtr m_pConn;
    	_RecordsetPtr m_pRs;
    	CString m_szTable;
    	CString m_szWhere;
    	CString m_szOrder;
    	CString m_szSQL;
    	CString m_szField;
    
    	_RecordsetPtr Select();
    	void Query(CString szSQL);
    	void InitQuery(void);
    	void Trace(void);
    	void Delete(void);
    	void ErrorQuery(_com_error& e);
    	DWORD GetCount(void);
    	_variant_t GetField(_variant_t szField);
    	void MoveNext(void);
    	void Close(void);
    };
    
    #include "StdAfx.h"
    #include "MyAdoConn.h"
    
    CMyAdoConn::CMyAdoConn(void)
    : m_szDBFile(_T(""))
    , m_szTable(_T(""))
    , m_szWhere(_T(""))
    , m_szOrder(_T(""))
    , m_szSQL(_T(""))
    , m_szField(_T("*"))
    {
    
    }
    
    CMyAdoConn::~CMyAdoConn(void)
    {
    }
    
    void CMyAdoConn::SetDBFile(CString szDBFile)
    {
    	m_szDBFile = szDBFile;
    }
    
    void CMyAdoConn::DBConn(void)
    {
    	try
    	{
    		HRESULT hr = CoInitialize(0);
    		_bstr_t strProvider(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_szDBFile);
    	    
    		m_pConn.CreateInstance(L"ADODB.Connection");
    		hr = m_pConn->Open(strProvider,L"",L"",adConnectUnspecified);
    	}
    	catch(_com_error& e)
    	{
    		ErrorQuery(e);
    	}
    }
    
    _RecordsetPtr CMyAdoConn::Select()
    {
    	try
    	{
    		HRESULT hr = CoInitialize(0);
    
    		m_pRs.CreateInstance(L"ADODB.Recordset"); 
    		m_pRs->CursorLocation = adUseClient;
    
    		m_szSQL = L"SELECT " + m_szField + L" FROM " + m_szTable;
    		if(m_szWhere != L"")
    			m_szSQL += L" WHERE " + m_szWhere;
    		if(m_szOrder != "")
    			m_szSQL += L" ORDER BY " + m_szOrder;
    		
    		hr = m_pRs->Open((LPCTSTR)m_szSQL, _variant_t((IDispatch *)m_pConn,true),
    						/*adOpenDynamic*/adOpenKeyset, /*adLockBatchOptimistic*/adLockReadOnly, adCmdText);
    	}
    	catch(_com_error& e)
    	{
    		ErrorQuery(e);
    	}
    	return m_pRs;
    }
    
    void CMyAdoConn::InitQuery(void)
    {
    	m_szTable = m_szWhere = m_szOrder = m_szSQL = "";
    	m_szField = L"*";
    }
    
    void CMyAdoConn::Query(CString szSQL)
    {
    	try
    	{
    		m_szSQL = szSQL;
    		_CommandPtr pCmd = NULL;
    		HRESULT hr = CoInitialize(0);
    
    		pCmd.CreateInstance(__uuidof(Command));
    		pCmd->ActiveConnection = m_pConn;
    		pCmd->CommandText = m_szSQL.AllocSysString();
    			
    		hr = pCmd->Execute(NULL, NULL, adCmdText);
    	}
    	catch(_com_error& e)
    	{
    		ErrorQuery(e);
    	}
    }
    
    void CMyAdoConn::Trace(void)
    {
    	TRACE(L"DB QUERY : %s\n", m_szSQL);
    }
    
    void CMyAdoConn::Delete(void)
    {
    	try
    	{
    		m_szSQL = L"DELETE FROM " + m_szTable;
    		if(m_szWhere != L"")
    			m_szSQL += L" WHERE " + m_szWhere;
    
    		_CommandPtr pCmd = NULL;
    		HRESULT hr = CoInitialize(0);
    
    		pCmd.CreateInstance(__uuidof(Command));
    		pCmd->ActiveConnection = m_pConn;
    		pCmd->CommandText = m_szSQL.AllocSysString();
    
    		hr = pCmd->Execute(NULL, NULL, adCmdText);
    	}
    	catch(_com_error& e)
    	{
    		ErrorQuery(e);
    	}
    }
    
    void CMyAdoConn::ErrorQuery(_com_error& e)
    {
    	_bstr_t bstrDescription(e.Description());
    	AfxMessageBox(bstrDescription + L"\n" + m_szSQL);
    	exit(1);
    }
    
    DWORD CMyAdoConn::GetCount(void)
    {
    	return m_pRs->RecordCount;
    }
    
    _variant_t CMyAdoConn::GetField(_variant_t szField)
    {
    	return m_pRs->Fields->GetItem(szField)->Value;
    }
    
    void CMyAdoConn::MoveNext(void)
    {
    	m_pRs->MoveNext();
    }
    
    void CMyAdoConn::Close(void)
    {
    	m_pRs->Close();
    }

    Comment


    입력하3 1508772051



    Locations of visitors to this page