📄 datasource.cpp
字号:
// DataSource.cpp: implementation of the CDataSource class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "LULU.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()
{
CoInitialize(NULL);
m_pConn.CreateInstance(__uuidof(Connection)); //m_pConn.CreateInstance("ADODB.Connection");
m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\温昌旺--请务必别删除--谢谢\\璐\\biaoqing.mdb","","",adModeUnknown);
//m_pRecordset->Open("Select CLASS as MAXID From 表1",_variant_t(m_pConn,true),adOpenStatic,adLockOptimistic,adCmdText);
//m_MaxID=GetAsInteger("MAXID");
//m_pRecordset->Close();
m_pRecordset->Open("Select*From biaoqing ",_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::MoveNext()
{
m_pRecordset->MoveNext();
}
void CDataSource::MoveLast()
{
m_pRecordset->MoveLast();
}
void CDataSource::MovePrev()
{
m_pRecordset->MovePrevious();
}
BOOL CDataSource::IsFirst()
{
BOOL Result;
if(m_pRecordset->BOF)
{
return TRUE;
}
else
{
m_pRecordset->MovePrevious();
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;
}
}
BOOL CDataSource::IsBOF()
{
return m_pRecordset->BOF;
}
BOOL CDataSource::IsEOF()
{
return m_pRecordset->EndOfFile;
}
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"";
}
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)
{
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;
}
else
{
return atoi(_bstr_t(vValue));
}
}
void CDataSource::SetAsString(CString FieldName,CString Value)
{
LPTSTR lpFieldName=FieldName.GetBuffer(FieldName.GetLength());
LPTSTR lpValue=Value.GetBuffer(Value.GetLength());
m_pRecordset->Fields->Item[lpFieldName]->Value=lpValue;
FieldName.ReleaseBuffer();
Value.ReleaseBuffer();
}
void CDataSource::SetAsInteger(CString FieldName,int Value)
{
CString cs;
cs.Format("%d",Value);//将Value由int型转为CString型
SetAsString(FieldName,cs);//使用SetAsString设置指定列的值
}
void CDataSource::New(CString Number,CString Class )
{
CString strTemp;
m_pRecordset->AddNew();
//m_MaxID++;
//SetAsInteger("ID",m_MaxID);
SetAsString("NUMBER",Number);
SetAsString("CLASS",Class);
// SetAsString("VOICE","OK");
// SetAsString("EMOTIOM","满意");
m_pRecordset->Update();
}
void CDataSource::Update()
{
m_pRecordset->Update();
}
void CDataSource::Delete()
{
m_pRecordset->Delete(adAffectCurrent);
m_pRecordset->MoveFirst();
// m_pRecordset->Update();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -