【VC开源代码栏目提醒】:以下是网学会员为您推荐的VC开源代码-AdoStudentDoc.cpp,希望本篇文章对您学习有所帮助。
// AdoStudentDoc.cpp : CAdoStudentDoc 类的实现
//
#include "stdafx.h"
#include "AdoStudent.h"
#include "StudentRs.h"
#include "AdoStudentDoc.h"
#include "AdoStudentView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAdoStudentDoc
IMPLEMENT_DYNCREATE(CAdoStudentDoc, CDocument)
BEGIN_MESSAGE_MAP(CAdoStudentDoc, CDocument)
ON_UPDATE_COMMAND_UI(ID_PREV_RECORD, OnUpdatePrevRecord)
ON_UPDATE_COMMAND_UI(ID_FIRST_RECORD, OnUpdateFirstRecord)
ON_UPDATE_COMMAND_UI(ID_NEXT_RECORD, OnUpdateNextRecord)
ON_UPDATE_COMMAND_UI(ID_LAST_RECORD, OnUpdateLastRecord)
END_MESSAGE_MAP()
// CAdoStudentDoc 构造/销毁
CAdoStudentDoc::CAdoStudentDoc()
: m_piAdoRecordBinding(NULL)
, m_strConnection(_T(""))
, m_strCmdText(_T(""))
{
// TODO:在此添加一次性构造代码
}
CAdoStudentDoc::~CAdoStudentDoc()
{
}
BOOL CAdoStudentDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO:在此添加重新初始化代码
// (SDI 文档将重用该文档)
//(SDI documents will reuse this document)
//Set the connection and SQL command strings
m_strConnection = _T("Provider = MSDASQL.1;Data Source = Student");
m_strCmdText = _T("select * from Student");
//Initialze the Recordset and binding pointers
m_pRs = NULL;
m_piAdoRecordBinding = NULL;
//Initialize the COM envirionment.
::CoInitialize(NULL);
try
{
//Create the record set object
m_pRs.CreateInstance(__uuidof(Recordset));
//Open the record set object
m_pRs->Open((LPCTSTR)m_strCmdText,(LPCTSTR)m_strConnection,
adOpenDynamic,adLockOptimistic,adCmdUnknown);
//Get a pointer to the record binding interface
if(FAILED(m_pRs->QueryInterface(__uuidof(IADORecordBinding),
(LPVOID*)&m_piAdoRecordBinding)))
_com_issue_error(E_NOINTERFACE);
//Bind the record class to the record set
m_piAdoRecordBinding->BindToRecordset(&m_rsRecSet);
//Get a pointer to the view
POSITION pos = GetFirstViewPosition();
CAdoStudentView* pView = (CAdoStudentView*)GetNextView(pos);
if(pView)
//sync the data set with the form.
pView->RefreshBoundData();
}
//Any errors?
catch(_com_error &e)
{
//Display the error
GenerateError(e.Error(),e.Description());
}
return TRUE;
}
// CAdoStudentDoc 序列化
void CAdoStudentDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO:在此添加存储代码
}
else
{
// TODO:在此添加加载代码
}
}
// CAdoStudentDoc 诊断
#ifdef _DEBUG
void CAdoStudentDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CAdoStudentDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CAdoStudentDoc 命令
CStudentRs* CAdoStudentDoc::GetRecSet(void)
{
//Return a pointer to the record object
return &m_rsRecSet;
}
void CAdoStudentDoc::GenerateError(HRESULT hr, PWSTR pwszDescription)
{
CString strError;
//Format and display the error message.
strError.Format("Run-time error'%d(%x)'",hr,hr);
strError += "\n\n";
strError += pwszDescription;
AfxMessageBox(strError);
}
void CAdoStudentDoc::DeleteContents()
{
// TODO:在此添加专用代码和/或调用基类
//Close the record set.
if(m_pRs)
m_pRs->Close();
//Do we have a valid pointer to the record binding?
if(m_piAdoRecordBinding)
//Release it
m_piAdoRecordBinding->Release();
//Set the record set pointer to NULL.
m_pRs = NULL;
//Shut down the COM environment
CoUninitialize();
CDocument::DeleteContents();
}
void CAdoStudentDoc::MoveFirst(void)
{
try
{
//Update the current record.
m_piAdoRecordBinding->Update(&m_rsRecSet);
//Move to the first record.
m_pRs->MoveFirst();
}
//Any errors?
catch(_com_error &e)
{
//Generate the error message.
GenerateError(e.Error(),e.Description());
}
}
void CAdoStudentDoc::MoveLast(void)
{
try
{
//Update the current record
m_piAdoRecordBinding->Update(&m_rsRecSet);
//Move to the last record.
m_pRs->MoveLast();
}
//Any errors?
catch(_com_error &e)
{
//Generate the error message.
GenerateError(e.Error(),e.Description());
}
}
void CAdoStudentDoc::MoveNext(void)
{
try
{
//Update the current record
m_piAdoRecordBinding->Update(&m_rsRecSet);
//Move to the last record.
m_pRs->MoveNext();
}
//Any errors?
catch(_com_error &e)
{
//Generate the error message.
GenerateError(e.Error(),e.Description());
}
}
void CAdoStudentDoc::MovePrevious(void)
{
try
{
//Update the current record
m_piAdoRecordBinding->Update(&m_rsRecSet);
//Move to the last record.
m_pRs->MovePrevious();
}
//Any errors?
catch(_com_error &e)
{
//Generate the error message.
GenerateError(e.Error(),e.Description());
}
}
void CAdoStudentDoc::AddNew(void)
{
try
{
//Update the current record
m_piAdoRecordBinding->Update(&m_rsRecSet);
//Can we add a new record?
if(m_pRs->Supports(adAddNew))
{
//Create a blank record
CreateBlankRecord();
//Add the blank record.
m_piAdoRecordBinding->AddNew(&m_rsRecSet);
//Move to the last record.
m_pRs->MoveLast();
}
}
//Any errors?
catch(_com_error &e)
{
//Generate an error message.
GenerateError(e.Error(),e.Description());
}
}
void CAdoStudentDoc::CreateBlankRecord(void)
{
//Create the blank values to be used.
CString strBlank = "";
//Set each of the values in the record object.
m_rsRecSet.m_lStudentID = 0;
strcpy(m_rsRecSet.m_szStudentName,(LPCTSTR)strBlank);
m_rsRecSet.m_lClassID = 0;
strcpy(m_rsRecSet.m_szAddress,(LPCTSTR)strBlank);
strcpy(m_rsRecSet.m_szBirthdate,(LPCTSTR)strBlank);
}
void CAdoStudentDoc::Delete(void)
{
try
{
//Update the current record
m_piAdoRecordBinding->Update(&m_rsRecSet);
//Can we delete a record.
if(m_pRs->Supports(adDelete))
{
//Make sure the user wants to delete this record.
if(AfxMessageBox("你真的想要删除该记录吗?",
MB_YESNO|MB_ICONQUESTION) == IDYES)
{
//Delete the record.
m_pRs->Delete(adAffectCurrent);
//Move to the previous record.
m_pRs->MovePrevious();
}
}
}
//Any errors?
catch(_com_error &e)
{
//Generate an error message.
GenerateError(e.Error(),e.Description());
}
}
void CAdoStudentDoc::CanMovePrev(CCmdUI* pCmdUI)
{
//Does the record set exist?
if(m_pRs)
{
//Are we at the BOF?
if(m_pRs->BOF)
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
}
void CAdoStudentDoc::OnUpdatePrevRecord(CCmdUI *pCmdUI)
{
// TODO:在此添加命令更新 UI 处理程序代码
CanMovePrev(pCmdUI);
}
void CAdoStudentDoc::OnUpdateFirstRecord(CCmdUI *pCmdUI)
{
// TO
上一篇:
AdoStudent.cpp
下一篇:
计算机仿真论文1