2009-07-27
展开全部
/ DataSource.cpp: implementation of the CDataSource class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.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::DataConnection()
{
//初始化Com对象,为使用ADO做准备
CoInitialize(NULL);
//初始化连接对象
m_pConn.CreateInstance("ADODB.Connection");
//初始化记录集对象
//m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
//打开数据库连接
m_pConn->Open(";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::DataOpen(CString SQL)
{
//初始化记录集对象
m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
m_pRecordset->Open(_variant_t(SQL), _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);
//return TRUE;
}
catch(_com_error &e)
{
::AfxMessageBox(e.ErrorMessage());
//return FALSE;
}
}
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_MaxID
m_pRecordset->Close();
m_pRecordset->Open("Select Max(ID) as MAXID From banpiantezheng", _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);
m_MaxID = GetAsInteger("MAXID");
m_pRecordset->Close();
//打开指定记录集
m_pRecordset->Open("Select * From banpiantezheng", _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);
//添加一条新的记录
m_pRecordset->AddNew();
//赋默认值
m_MaxID++;
SetAsInteger("ID", m_MaxID);
SetAsString("规格型号", "");
SetAsString("平均流道横截面积f", "");
SetAsString("Ah", "");
SetAsString("Bh","");
SetAsString("C","");
SetAsString("d","");
SetAsString("Ac","");
SetAsString("Bc","");
SetAsString("Dx", "");
SetAsString("平均当量直径","");
SetAsString("单板换热面积","");
SetAsString("pa","");
SetAsString("pb","");
SetAsString("流道孔直径","");
SetAsString("最小装机片数","");
SetAsString("最大装机片数","");
SetAsString("板片厚度","");
SetAsString("pd", "");
//更新
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;
}
void CDataSource::Close()
{
m_pRecordset->Close();
}
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.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::DataConnection()
{
//初始化Com对象,为使用ADO做准备
CoInitialize(NULL);
//初始化连接对象
m_pConn.CreateInstance("ADODB.Connection");
//初始化记录集对象
//m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
//打开数据库连接
m_pConn->Open(";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::DataOpen(CString SQL)
{
//初始化记录集对象
m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
m_pRecordset->Open(_variant_t(SQL), _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);
//return TRUE;
}
catch(_com_error &e)
{
::AfxMessageBox(e.ErrorMessage());
//return FALSE;
}
}
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_MaxID
m_pRecordset->Close();
m_pRecordset->Open("Select Max(ID) as MAXID From banpiantezheng", _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);
m_MaxID = GetAsInteger("MAXID");
m_pRecordset->Close();
//打开指定记录集
m_pRecordset->Open("Select * From banpiantezheng", _variant_t(m_pConn, true), adOpenStatic, adLockOptimistic, adCmdText);
//添加一条新的记录
m_pRecordset->AddNew();
//赋默认值
m_MaxID++;
SetAsInteger("ID", m_MaxID);
SetAsString("规格型号", "");
SetAsString("平均流道横截面积f", "");
SetAsString("Ah", "");
SetAsString("Bh","");
SetAsString("C","");
SetAsString("d","");
SetAsString("Ac","");
SetAsString("Bc","");
SetAsString("Dx", "");
SetAsString("平均当量直径","");
SetAsString("单板换热面积","");
SetAsString("pa","");
SetAsString("pb","");
SetAsString("流道孔直径","");
SetAsString("最小装机片数","");
SetAsString("最大装机片数","");
SetAsString("板片厚度","");
SetAsString("pd", "");
//更新
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;
}
void CDataSource::Close()
{
m_pRecordset->Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |