MFC ADO 连接使用类
ADOConn.h:
ADOCOnn.cpp:
Code
// ADOConn.h: interface for the ADOConn class.
//
/**///////////////////////////////////////////////////////////////////////
#if !defined(AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_)
#define AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "adoEOF")rename("BOF", "adoBOF")
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class ADOConn
{
public:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
public:
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
ADOConn();
virtual ~ADOConn();
void OnInitADOConn();
BOOL ExecuteSQL(_bstr_t bstrSQL);
void ExitConnect();
};
#endif // !defined(AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_)
// ADOConn.h: interface for the ADOConn class.
//
/**///////////////////////////////////////////////////////////////////////
#if !defined(AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_)
#define AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "adoEOF")rename("BOF", "adoBOF")
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class ADOConn
{
public:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
public:
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
ADOConn();
virtual ~ADOConn();
void OnInitADOConn();
BOOL ExecuteSQL(_bstr_t bstrSQL);
void ExitConnect();
};
#endif // !defined(AFX_ADOCONN_H__5FB9A9B2_8D94_44F7_A2DA_1F37A4F33D10__INCLUDED_)
ADOCOnn.cpp:
Code
#include "StdAfx.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
void ADOConn::OnInitADOConn()
{
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->ConnectionString="File Name=SQL.UDL";
m_pConnection->Open("","","",NULL);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
e.Description();
return false;
}
}
void ADOConn::ExitConnect()
{
if(m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
}
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
e.Description();
}
return m_pRecordset;
}
#include "StdAfx.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
void ADOConn::OnInitADOConn()
{
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->ConnectionString="File Name=SQL.UDL";
m_pConnection->Open("","","",NULL);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
e.Description();
return false;
}
}
void ADOConn::ExitConnect()
{
if(m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
}
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
e.Description();
}
return m_pRecordset;
}