⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ado.cpp

📁 基于com的网络爬虫程序
💻 CPP
字号:
//
// This file is part of UniWebSpider Project.
//
// UniWebSpider is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// UniWebSpider is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with UniWebSpider; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

// WRITTEN BY	: ValGarn

// ADO.cpp: implementation of the CADO class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <afx.h>
#include "Dispatch.h"
#include "ADO.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CADO::CADO()
{
	try
	{
		CoInitialize(NULL);
		m_Command.CreateInstance(__uuidof(Command));
		m_Connection.CreateInstance(__uuidof(Connection));
	}
	catch(_com_error e) { }
	catch(...) 	{ }
}

CADO::~CADO()
{
	try
	{
		if(m_Connection->State == adStateOpen) m_Connection->Close();
		m_Command = NULL;
		m_Connection = NULL;
	}
	catch(_com_error e){ }
	catch(...){ }
	CoUninitialize();
}

BOOL CADO::CloseConnection()
{
	try
	{
		if(m_Connection->State == adStateOpen)
			m_Connection->Close();
	}
	catch(_com_error e)
	{ 
		return FALSE;
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

BOOL CADO::ExecuteCommand(BSTR SQL)
{
	VARIANT RecordsEffected;
	RecordsEffected.vt = VT_INT;
	try
	{
		m_Connection->Execute(_bstr_t(SQL),&RecordsEffected,adExecuteNoRecords);
	}
	catch(_com_error e)
	{ 
		return FALSE;
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}


BOOL CADO::Execute(BSTR SQL, _Recordset **Result)
{
	VARIANT RecordsEffected;
	RecordsEffected.vt = VT_INT;
	try
	{
		_RecordsetPtr PopSet;
		PopSet.CreateInstance(__uuidof(Recordset));
		if(PopSet->State == adStateOpen) PopSet->Close();
		PopSet = m_Connection->Execute(_bstr_t(SQL),&RecordsEffected,adCmdText);
		*Result = PopSet.Detach();
	}
	catch(_com_error e)
	{ 
		return FALSE;
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

BOOL CADO::ExecuteReturnDelimited(BSTR SQL, BSTR Delimiter, VARIANT *Result)
{
	VARIANT RecordsEffected;
	RecordsEffected.vt = VT_INT;
	*Result = _variant_t("");
	try
	{
		_RecordsetPtr PopSet;
		PopSet.CreateInstance(__uuidof(Recordset));
		PopSet->CursorLocation = adUseClient;
		if(m_Connection->State != adStateOpen)
		{
			*Result = _variant_t("VISP_COM_ERROR_CONNECTION_NOT_OPEN");
			return S_FALSE;
		}
		Execute(_bstr_t(SQL),&PopSet);
		if(!PopSet->adoEOF)
		{
			BSTR bstrResult = NULL;
			_bstr_t btColDelim(L","); 
			_bstr_t btRowDelim(L"\r\n"); 
			_bstr_t btNullExp(L""); 
			PopSet->raw_GetString(adClipString,-1,btColDelim,btRowDelim,btNullExp,&bstrResult);
			*Result = _variant_t(bstrResult);
		}
		else
			*Result = _variant_t("");
	}
	catch(_com_error e)
	{ 
		return FALSE;
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

BOOL CADO::ExecuteSP(BSTR SPName, VARIANT ParameterString, _Recordset **Result)
{
	VARIANT RecordsEffected;
	RecordsEffected.vt = VT_INT;
	_RecordsetPtr PopSet;	
	PopSet.CreateInstance(__uuidof(Recordset));
	if(PopSet->State == adStateOpen) PopSet->Close();
	try
	{
		m_Command->ActiveConnection = m_Connection;
		m_Command->CommandText = _bstr_t(SPName);
		PopSet = m_Command->Execute(&RecordsEffected,&ParameterString,adCmdStoredProc);
		*Result = PopSet.Detach();
	}
	catch(_com_error e)
	{ 
		CString s = e.ErrorMessage();
		return FALSE;
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

BOOL CADO::ExecuteSPReturnDelimited(BSTR SPName, VARIANT ParameterArray, BSTR Delimiter, VARIANT *Result)
{
	VARIANT RecordsEffected;
	RecordsEffected.vt = VT_INT;
	*Result = _variant_t("");
	try
	{
		_RecordsetPtr PopSet;
		PopSet.CreateInstance(__uuidof(Recordset));
		PopSet->CursorLocation = adUseClient;
		if(m_Connection->State != adStateOpen)
		{
			*Result = _variant_t("COM_ERROR_CONNECTION_NOT_OPEN");
			return S_FALSE;
		}
		m_Command->ActiveConnection = m_Connection;
		m_Command->CommandText = _bstr_t(SPName);
		m_Command->CommandType = adCmdStoredProc;		

		PopSet = m_Command->Execute(&RecordsEffected,&ParameterArray,adCmdUnknown);
		if(!PopSet->adoEOF)
		{
			BSTR bstrResult = NULL;
			_bstr_t btColDelim(Delimiter); 
			_bstr_t btRowDelim(L"\r\n"); 
			_bstr_t btNullExp(L""); 
			PopSet->raw_GetString(adClipString,-1,btColDelim,btRowDelim,btNullExp,&bstrResult);
			*Result = _variant_t(bstrResult);
		}
		else
			*Result = _variant_t("");
	}
	catch(_com_error e)
	{ 
		return FALSE;
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

BOOL CADO::OpenConnection(BSTR ConnectionString, BSTR UserId, BSTR Password)
{
	try
	{			
		m_Connection->Open(ConnectionString, UserId, Password, adConnectUnspecified);
	}
	catch(_com_error e)
	{ 
		return FALSE;
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -