几何尺寸与公差论坛

 找回密码
 注册
查看: 6201|回复: 3

Adoaccessor.dsw工程

[复制链接]
发表于 2007-7-22 15:32:29 | 显示全部楼层 |阅读模式
; ADOAccessor.def : Declares the module parameters.

LIBRARY      "ADOAccessor.DLL"

EXPORTS
DllCanUnloadNow     @1 PRIVATE
DllGetClassObject   @2 PRIVATE
DllRegisterServer   @3 PRIVATE
DllUnregisterServer @4 PRIVATE
 楼主| 发表于 2007-7-22 15:38:35 | 显示全部楼层

回复: Adoaccessor.dsw工程

2. stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
#if !defined(AFX_STDAFX_H__667E9AB8_BDD9_4C7F_AEBD_9FADD0E3AE4C__INCLUDED_)
#define AFX_STDAFX_H__667E9AB8_BDD9_4C7F_AEBD_9FADD0E3AE4C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define STRICT
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#define _ATL_APARTMENT_THREADED
#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>
#import "E:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__667E9AB8_BDD9_4C7F_AEBD_9FADD0E3AE4C__INCLUDED)
 楼主| 发表于 2007-7-22 15:50:17 | 显示全部楼层

回复: Adoaccessor.dsw工程

3.ADOTier.h
// ADOTier.h : Declaration of the CADOTier
#ifndef __ADOTIER_H_
#define __ADOTIER_H_

#include "resource.h" // main symbols

/////////////////////////////////////////////////////////////////////////////
// CADOTier
class ATL_NO_VTABLE CADOTier :
public CComObjectRootEx<CComSingleThreadModel>,
public ISupportErrorInfo,
public CComCoClass<CADOTier, &CLSID_ADOTier>,
public IDispatchImpl<IADOTier, &IID_IADOTier, &LIBID_ADOACCESSORLib>
{
public:
CADOTier()
{
}

DECLARE_REGISTRY_RESOURCEID(IDR_ADOTIER)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CADOTier)
COM_INTERFACE_ENTRY(IADOTier)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()

// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

// IADOTier
public:
STDMETHOD(get_Empty)(/*[out, retval]*/ VARIANT_BOOL *pVal);
STDMETHOD(ADORelease)();
STDMETHOD(Requery)();
STDMETHOD(ChangeParameter)(/*[in]*/ long idx, /*[in]*/ enum DataTypeEnum type, /*[in]*/ VARIANT value, /*[in]*/ enum ParameterDirectionEnum where, /*[in]*/ long size);
STDMETHOD(put_StoredProc)(/*[in]*/ BSTR newVal);
STDMETHOD(CallStoredProc)(/*[in]*/ long idx1, /*[in]*/ BSTR idx2, /*[in]*/ BSTR idx3);
STDMETHOD(ParamQuery)(/*[in]*/ BSTR query, /*[in]*/ long idx1, /*[in]*/ BSTR idx2, /*[in]*/ BSTR idx3);
STDMETHOD(get_BOF)(/*[out, retval]*/ VARIANT_BOOL *pVal);
STDMETHOD(get_EOF)(/*[out, retval]*/ VARIANT_BOOL *pVal);
STDMETHOD(Prev)();
STDMETHOD(Last)();
STDMETHOD(Next)();
STDMETHOD(First)();
STDMETHOD(Close)();
STDMETHOD(get_FieldCount)(/*[out, retval]*/ long *pVal);
STDMETHODIMP put_FieldCount(/*[in]*/ long pVal);
STDMETHOD(get_Field)(/*[in]*/ VARIANT idx, /*[out, retval]*/ VARIANT *pVal);
STDMETHOD(put_Field)(/*[in]*/ VARIANT idx, /*[in]*/ VARIANT newVal);
STDMETHOD(Delete)();
STDMETHOD(Update)();
STDMETHOD(AppendParameter)(/*[in]*/ enum DataTypeEnum type, /*[in]*/ VARIANT value, /*[in]*/ enum ParameterDirectionEnum where, /*[in]*/ long size);
STDMETHOD(get_CommandText)(/*[out, retval]*/ BSTR *pVal);
STDMETHOD(put_CommandText)(/*[in]*/ BSTR newVal);
STDMETHOD(ExecuteCommand)(/*[in]*/ VARIANT_BOOL bStoredProcedure, /*[in]*/ VARIANT_BOOL bChangeRec);
STDMETHOD(ExecuteConnection)(/*[in]*/ BSTR query, /*[in]*/ VARIANT_BOOL bChangeRec);
STDMETHOD(CloseRecordset)(void);
STDMETHOD(OpenRecordset)(/*[in]*/ VARIANT query);
STDMETHOD(Open)(/*[in]*/ BSTR source, /*[in]*/ BSTR user, /*[in]*/ BSTR pwd);
private:
_CommandPtr m_command;
_RecordsetPtr m_recordset;
_ConnectionPtr m_connection;
};

#endif //__ADOTIER_H_
 楼主| 发表于 2007-7-22 15:51:07 | 显示全部楼层

回复: Adoaccessor.dsw工程

4.ADOTier.h
// ADOTier.cpp : Implementation of CADOTier
#include "stdafx.h"
#include "ADOAccessor.h"
#include "ADOTier.h"
/////////////////////////////////////////////////////////////////////////////
// Need two distinct "empty" VARIANTs for Command::Execute
static VARIANT* pvtEmpty = static_cast<VARIANT*> (&vtMissing);
static _variant_t vtMissing2(DISP_E_PARAMNOTFOUND, VT_ERROR);
static VARIANT* pvtEmpty2 = static_cast<VARIANT*> (&vtMissing2);
STDMETHODIMP CADOTier::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_IADOTier,
};
for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
{
if (InlineIsEqualGUID(*arr,riid))
return S_OK;
}
return S_FALSE;
}

STDMETHODIMP CADOTier::Open(BSTR source, BSTR user, BSTR pwd)
{
HRESULT hr = m_connection.CreateInstance(__uuidof(Connection));
if (SUCCEEDED(hr))
hr = m_connection->Open(source, user, pwd, adConnectUnspecified);
if (SUCCEEDED(hr))
hr = m_command.CreateInstance(__uuidof(Command));
if (SUCCEEDED(hr))
hr = m_recordset.CreateInstance(__uuidof(Recordset));
return hr;
}
STDMETHODIMP CADOTier::OpenRecordset(VARIANT query)
{
VARIANT v;
V_VT(&v) = VT_DISPATCH;
V_DISPATCH(&v) = (IDispatch*) m_connection;
// Need the AddRef() as VariantClear() calls Release(), unless fAddRef
// false indicates we're taking ownership
//
V_DISPATCH(&v)->AddRef();
return m_recordset->Open(query, v, adOpenDynamic, adLockOptimistic, adCmdText);
}
STDMETHODIMP CADOTier::CloseRecordset()
{
return m_recordset->Close();
}
STDMETHODIMP CADOTier::ExecuteConnection(BSTR query, VARIANT_BOOL bChangeRec)
{
_Recordset* prec = 0;
HRESULT hr = S_OK;
prec = m_connection->Execute(query, pvtEmpty, adCmdText);
if (prec)
{
if (bChangeRec)
m_recordset = prec;
else
prec->Release();
}
return hr;
}
STDMETHODIMP CADOTier::ExecuteCommand(VARIANT_BOOL bStoredProcedure, VARIANT_BOOL bChangeRec)
{
_Recordset* prec = 0;
HRESULT hr = S_OK;
if (bStoredProcedure)
prec = m_command->Execute(pvtEmpty, pvtEmpty2, adCmdStoredProc);
else
prec = m_command->Execute(pvtEmpty, pvtEmpty2, adCmdText);
if (prec)
{
if (bChangeRec)
m_recordset = prec;
else
prec->Release();
}
return hr;
}
STDMETHODIMP CADOTier::get_CommandText(BSTR *pVal)
{
return m_command->get_CommandText(pVal);
}
STDMETHODIMP CADOTier::put_CommandText(BSTR newVal)
{
return m_command->put_CommandText(newVal);
}
STDMETHODIMP CADOTier::AppendParameter(enum DataTypeEnum type, VARIANT value, enum ParameterDirectionEnum where, long size)
{
_ParameterPtr param;
HRESULT hr = param.CreateInstance(__uuidof(Parameter));
if (SUCCEEDED(hr))
hr = param->put_Type(type);
if (SUCCEEDED(hr))
hr = param->put_Value(value);
if (SUCCEEDED(hr))
hr = param->put_Direction(where);
if (SUCCEEDED(hr))
hr = param->put_Size(size);
Parameters* params = 0;
if (SUCCEEDED(hr))
hr = m_command->get_Parameters(&params);
if (SUCCEEDED(hr))
hr = params->Append(param);
if (SUCCEEDED(hr))
{
params->Release();
param->Release();
}
return hr;
}
STDMETHODIMP CADOTier::Update()
{
return m_recordset->Update();
}
STDMETHODIMP CADOTier:elete()
{
return m_recordset->Delete(adAffectCurrent);
}
STDMETHODIMP CADOTier::get_Field(VARIANT idx, VARIANT *pVal)
{
Fields* fields = 0;
HRESULT hr = m_recordset->get_Fields(&fields);
Field* field = 0;
if (SUCCEEDED(hr))
hr = fields->get_Item(idx, &field);
if (SUCCEEDED(hr))
hr = field->get_Value(pVal);
if (SUCCEEDED(hr))
{
fields->Release();
field->Release();
}
return hr;
}
STDMETHODIMP CADOTier::put_Field(VARIANT idx, VARIANT newVal)
{
Fields* fields = 0;
HRESULT hr = m_recordset->get_Fields(&fields);
Field* field = 0;
if (SUCCEEDED(hr))
hr = fields->get_Item(idx, &field);
if (SUCCEEDED(hr))
hr = field->put_Value(newVal);
if (SUCCEEDED(hr))
{
fields->Release();
field->Release();
}
return hr;
}
STDMETHODIMP CADOTier::get_FieldCount(long *pVal)
{
Fields* fields = 0;
HRESULT hr = m_recordset->get_Fields(&fields);
if (SUCCEEDED(hr))
hr = fields->get_Count(pVal);
if (SUCCEEDED(hr))
fields->Release();
return hr;
}
STDMETHODIMP CADOTier::put_FieldCount(long pVal)
{
return S_OK;
}
STDMETHODIMP CADOTier::Close()
{
return m_connection->Close();
}
STDMETHODIMP CADOTier::First()
{
return m_recordset->MoveFirst();
}
STDMETHODIMP CADOTier::Next()
{
return m_recordset->MoveNext();
}
STDMETHODIMP CADOTier:ast()
{
return m_recordset->MoveLast();
}
STDMETHODIMP CADOTier:rev()
{
return m_recordset->MovePrevious();
}
STDMETHODIMP CADOTier::get_EOF(VARIANT_BOOL *pVal)
{
return m_recordset->get_adoEOF(pVal);
}
STDMETHODIMP CADOTier::get_BOF(VARIANT_BOOL *pVal)
{
return m_recordset->get_BOF(pVal);
}
STDMETHODIMP CADOTier:aramQuery(BSTR query, long idx1, BSTR idx2, BSTR idx3)
{
HRESULT hr = ChangeParameter(0, adInteger, (_variant_t) idx1, adParamInput, -1);
if (SUCCEEDED(hr))
hr = ChangeParameter(1, adVarChar, (_variant_t) idx2, adParamInput, 25);
if (SUCCEEDED(hr))
hr = ChangeParameter(2, adVarChar, (_variant_t) idx3, adParamInput, 80);
if (SUCCEEDED(hr))
hr = m_command->put_CommandText(query);
_Recordset* prec = 0;
if (SUCCEEDED(hr))
prec = m_command->Execute(pvtEmpty, pvtEmpty2, adCmdText);
if (prec) prec->Release();
return hr;
}
STDMETHODIMP CADOTier::CallStoredProc(long idx1, BSTR idx2, BSTR idx3)
{
HRESULT hr = ChangeParameter(0, adInteger, (_variant_t) idx1, adParamInput, 4);
if (SUCCEEDED(hr))
hr = ChangeParameter(1, adVarChar, (_variant_t) idx2, adParamInput, 25);
if (SUCCEEDED(hr))
hr = ChangeParameter(2, adVarChar, (_variant_t) idx3, adParamInput, 80);
if (SUCCEEDED(hr))
hr = m_command->put_CommandText(L"{call MyProc (?, ?, ?)}");
_Recordset* prec = 0;
if (SUCCEEDED(hr))
prec = m_command->Execute(pvtEmpty, pvtEmpty2, adCmdText);
if (prec) prec->Release();
return hr;
}
STDMETHODIMP CADOTier::put_StoredProc(BSTR newVal)
{
if (newVal == NULL)
newVal = ::SysAllocString(L"create procedure MyProc @i integer, @g varchar(25), @g varchar(80) into Guns (ID, Gun, [Gun Description]) values (@i, @g, @d) return");
HRESULT hr = m_command->put_CommandText(newVal);
_Recordset* prec = 0;
if (SUCCEEDED(hr))
prec = m_command->Execute(pvtEmpty, pvtEmpty2, adCmdText);
if (prec) prec->Release();
return hr;
}
STDMETHODIMP CADOTier::ChangeParameter(long idx, enum DataTypeEnum type, VARIANT value, enum ParameterDirectionEnum where, long size)
{
Parameters* params = 0;
HRESULT hr = m_command->get_Parameters(&params);
_Parameter* param = 0;
VARIANT v;
V_VT(&v) = VT_I4;
V_I4(&v) = idx;
if (SUCCEEDED(hr))
hr = params->get_Item(v, &param);
if (SUCCEEDED(hr))
hr = param->put_Type(type);
if (SUCCEEDED(hr))
hr = param->put_Value(value);
if (SUCCEEDED(hr))
hr = param->put_Direction(where);
if (SUCCEEDED(hr))
hr = param->put_Size(size);
if (SUCCEEDED(hr))
{
params->Release();
param->Release();
}
return hr;
}
STDMETHODIMP CADOTier::Requery()
{
return m_recordset->Requery(adCmdText);
}
STDMETHODIMP CADOTier::ADORelease()
{
m_command = 0;
m_recordset = 0;
m_connection = 0;
return S_OK;
}
STDMETHODIMP CADOTier::get_Empty(VARIANT_BOOL *pVal)
{
HRESULT hr = m_recordset->get_adoEOF(pVal);
if (SUCCEEDED(hr) && pVal)
hr = m_recordset->get_BOF(pVal);
return hr;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 11:37 , Processed in 0.040304 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表