Maison  >  Article  >  base de données  >  MFC用ADO连接数据库(ACCESS)

MFC用ADO连接数据库(ACCESS)

WBOY
WBOYoriginal
2016-06-07 15:36:241519parcourir

Microsoft ActiveX Data Objects (ADO) 支持用于建立基于客户端/服务器和 Web 的应用程序的主要功能。其主要优点是易于使用、高速度、低内存支出和占用磁盘空间较少。本次封装的CadoInterface类仅针对MFC的使用,目的是优化对ADO的操作,避免频繁写try catch

           Microsoft ActiveX Data Objects (ADO) 支持用于建立基于客户端/服务器和 Web 的应用程序的主要功能。其主要优点是易于使用、高速度、低内存支出和占用磁盘空间较少。   本次封装的CadoInterface类仅针对MFC的使用,目的是优化对ADO的操作,避免频繁写try catch(…)以及在连库、开表、写数据、读数据等过程中一些重复性的工作。该类仅对一些常用的操作进行封装,用户可以根据需要进行修改和扩展。

            封装类主要包括:基本操作、增值操作、支持算法与支持结构。基本操作、增值操作、支持算法在CDataBase.h与CDataBase.cpp中声明定义。

       1.用#import指令引入ADO类型库

为了引入ADO类型库,需要在项目的stdafx.h文件中加入如下语句: #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")注意添加的位置在#endif //_AFX_NO_AFXCMN_SUPPORT之后

       2.将封装类加到工程中

         CDataBase.h代码如下:

class CDataSource  
{
public:
	//当前记录指针是否到了所有记录之后
	BOOL IsEOF();
	//当前记录指针是否到了所有记录之前
	BOOL IsBOF();
	//删除当前记录
	void Delete();
	//设置FieldName字段的值为Value(int型)
	void SetAsInteger(CString FieldName, int Value);
	//设置FieldName字段的值为Value(CString型)
	void SetAsString(CString FieldName, CString Value);
	//将记录的修改更新到<strong>数据库</strong>中
	void Update();
	//新增一条记录
	void New();
	//得到FieldName字段的值(int型)
	int GetAsInteger(CString FieldName);
	//得到FieldName字段的值(CString型)
	CString GetAsString(CString FieldName);
	//当前记录指针是否到了最后一条记录
	BOOL IsLast();
	//当前记录指针是否到了第一条记录
	BOOL IsFirst();
	//移动当前记录指针到下一条记录
	void MoveNext();
	//移动当前记录指针到上一条记录
	void MovePrev();
	//移动当前记录指针到最后一条记录
	void MoveLast();
	//移动当前记录指针到第一条记录
	void MoveFirst();
	//初始化数据
	void InitData();
	CDataSource();
	virtual ~CDataSource();
private:
	int m_MaxID;
	_RecordsetPtr m_pRecordset;
	_ConnectionPtr m_pConn;
	//释放数据
	void FreeData();
};

3.CDataBase.cpp如下:

<span></span><pre name="code" class="cpp">#include "DataSource.h"
CDataSource::CDataSource()
{
}
CDataSource::~CDataSource()
{
	FreeData();
}
void CDataSource::InitData()
{
	//初始化Com对象,为使用ADO做准备
	CoInitialize(NULL);
	//初始化<strong>连接</strong>对象
	m_pConn.CreateInstance("ADODB.Connection");
	//初始化记录集对象
	m_pRecordset.CreateInstance("ADODB.Recordset");
	try
	{
		//打开<strong>数据库</strong><strong>连接</strong>
		m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\Data\\Demo.mdb;Persist Security Info=False", "", "", adConnectUnspecified);
		
		//初始化m_MaxID
		m_pRecordset->Open("Select Max(ID) as MAXID From Profile", _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);		
		m_MaxID = GetAsInteger("MAXID");
		m_pRecordset->Close();
		//打开指定记录集
		m_pRecordset->Open("Select * From Profile", _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);
	}
	catch(_com_error &e)
	{
		::AfxMessageBox(e.ErrorMessage());
	}
}
void CDataSource::FreeData()
{
	if (m_pConn)
	{
		m_pConn->Close();
		m_pRecordset.Release();
		m_pConn.Release();
		CoUninitialize();
	}
}
void CDataSource::MoveFirst()
{
	m_pRecordset->MoveFirst();
}
void CDataSource::MoveLast()
{
	m_pRecordset->MoveLast();
}
void CDataSource::MovePrev()
{
	m_pRecordset->MovePrevious();
}
void CDataSource::MoveNext()
{
	m_pRecordset->MoveNext();
}
BOOL CDataSource::IsFirst()
{
	if (m_pRecordset->BOF)
	{
		return TRUE;
	}
	else
	{
		m_pRecordset->MovePrevious();
		BOOL Result = m_pRecordset->BOF;
		m_pRecordset->MoveNext();
		return Result;
	}
}
BOOL CDataSource::IsLast()
{
	if (m_pRecordset->EndOfFile)
	{
		return TRUE;
	}
	else
	{
		m_pRecordset->MoveNext();
		BOOL Result = m_pRecordset->EndOfFile;
		m_pRecordset->MovePrevious();
		return Result;
	}
}
CString CDataSource::GetAsString(CString FieldName)
{
	//如果在第一条记录之前或者最后一条记录之后,返回空
	if (IsBOF() || IsEOF())
		return "";
	
	LPTSTR lpFieldName = FieldName.GetBuffer(FieldName.GetLength());
	
	//得到当前记录指定列的值
	_variant_t vValue = m_pRecordset->Fields->Item[lpFieldName]->Value;
	//如果为空值则返回空
	if ((V_VT(&vValue) == VT_NULL) || (V_VT(&vValue) == VT_EMPTY))
	{
		return "";
	}
	//否则以字符串形式返回vValue的值
	else
	{
		CString strResult;
		LPTSTR lpResult = strResult.GetBuffer(strlen(_bstr_t(vValue)));
		strcpy(lpResult, _bstr_t(vValue));
		strResult.ReleaseBuffer();
		return strResult;
	}
}
int CDataSource::GetAsInteger(CString FieldName)
{
	//如果在第一条记录之前或者最后一条记录之后,返回0
	if (IsBOF() || IsEOF())
		return 0;
	LPTSTR lpFieldName = FieldName.GetBuffer(FieldName.GetLength());
	
	//得到当前记录指定列的值
	_variant_t vValue = m_pRecordset->Fields->Item[lpFieldName]->Value;
	
	//如果为空值则返回空
	if (V_VT(&vValue) == VT_NULL)
	{
		return 0;
	}
	//否则以int形式返回vValue的值
	else
	{
		return atoi(_bstr_t(vValue));
	}
}
void CDataSource::New()
{
	//添加一条新的记录
	m_pRecordset->AddNew();
	//设置初始值
	m_MaxID++;
	SetAsInteger("ID", m_MaxID);
	SetAsString("NAME", "无名氏");
	SetAsInteger("GENDER", 0);
	SetAsInteger("AGE", 24);
	SetAsString("NATIONALITY", "汉");
	SetAsString("ADDRESS", "");
	SetAsString("POSTCODE", "");
	SetAsString("NOTE", "");
	
	//更新
	m_pRecordset->Update();
}
void CDataSource::Update()
{
	m_pRecordset->Update();
}
void CDataSource::SetAsString(CString FieldName, CString Value)
{
	//将列名(FieldName)由CString转为LPTSTR型
	LPTSTR lpFieldName = FieldName.GetBuffer(FieldName.GetLength());
	
	//将Value由CString转为LPTSTR型
	LPTSTR lpValue = Value.GetBuffer(Value.GetLength());
	
	//将Value值更新到Recordset中
	m_pRecordset->Fields->Item[lpFieldName]->Value = lpValue;
	//释放缓冲区
	FieldName.ReleaseBuffer();	
	Value.ReleaseBuffer();	
}
void CDataSource::SetAsInteger(CString FieldName, int Value)
{
	CString cs;
	
	//将Value由int型转为CString型
	cs.Format("%d", Value);
	//使用SetAsString设置指定列的值
	SetAsString(FieldName, cs);
}
void CDataSource::Delete()
{
	//删除当前记录
	m_pRecordset->Delete(adAffectCurrent);	
}
BOOL CDataSource::IsBOF()
{
	return m_pRecordset->BOF;
}
BOOL CDataSource::IsEOF()
{
	return m_pRecordset->EndOfFile;
}







代码链接:点击打开链接

添加操作:new()->m_ds.SetAsString("NAME");->m_ds.Update();

删除操作:m_ds.delete()->m_ds.Update();(删除m_ds指向的记录集)

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn