📄 datasource.cpp
字号:
// DataSource.cpp: implementation of the CDataSource class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DBView.h"
#include "DataSource.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDataSource::CDataSource()
{
}
CDataSource::~CDataSource()
{
FreeData();
}
void CDataSource::InitData()
{
//初始化Com对象,为使用ADO做准备
CoInitialize(NULL);
//初始化连接对象
m_pConn.CreateInstance("ADODB.Connection");
//初始化记录集对象
m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
//打开数据库连接
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -