【VC开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了VC开源代码-odbc01View.cpp的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
// odbc01View.cpp : Codbc01View 类的实现
//
#include "stdafx.h"
#include "odbc01.h"
#include "odbc01Doc.h"
#include "odbc01View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Codbc01View
IMPLEMENT_DYNCREATE(Codbc01View, CFormView)
BEGIN_MESSAGE_MAP(Codbc01View, CFormView)
ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
END_MESSAGE_MAP()
// Codbc01View 构造/销毁
Codbc01View::Codbc01View()
: CFormView(Codbc01View::IDD)
, pDB(NULL)
{
// TODO: 在此处添加构造代码
}
Codbc01View::~Codbc01View()
{
}
void Codbc01View::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list01);
}
BOOL Codbc01View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
return CFormView::PreCreateWindow(cs);
}
void Codbc01View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
Codbc01Doc *pDoc=GetDocument();
pDB = &pDoc->m_dbCust;
m_list01.InsertColumn(1,"ID",LVCFMT_CENTER,60);
m_list01.InsertColumn(2,"",LVCFMT_CENTER,60);
m_list01.InsertColumn(3,"年龄",LVCFMT_CENTER,60);
}
// Codbc01View 诊断
#ifdef _DEBUG
void Codbc01View::AssertValid() const
{
CFormView::AssertValid();
}
void Codbc01View::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
Codbc01Doc* Codbc01View::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(Codbc01Doc)));
return (Codbc01Doc*)m_pDocument;
}
#endif //_DEBUG
// Codbc01View 消息处理程序
void Codbc01View::OnFileConnect()
{
// TODO: 在此添加命令处理
程序代码
Codbc01Doc *pDoc=GetDocument();
CString str;
str = "DSN=db1;UID=;PWD=";
bool connected = false;
try
{
pDoc->m_dbCust.OpenEx(_T(str),CDatabase::noOdbcDialog);//登录数据库
AfxMessageBox("连接成功");
connected = true;
}
catch(CDBException* e)
{
AfxMessageBox(e->m_strError);
e->Delete();
pDoc->m_dbCust.Close();
return;
}
if (connected){
CString QueryString = "select * from tab01";
CString strId,strName,strAge;
CRecordset *rc;
int nitem;
Codbc01Doc *pDoc= GetDocument();
rc=new CRecordset(&pDoc->m_dbCust);//创建记录集对象
rc->Open(CRecordset::forwardOnly,_T(QueryString));//打开记录集
while (!rc->IsEOF())
{
rc->GetFieldValue(0,strId);
rc->GetFieldValue(1,strName);
rc->GetFieldValue(2,strAge);
nitem = m_list01.GetItemCount();
nitem = m_list01.InsertItem(nitem,strId);
m_list01.SetItem(nitem,1,LVIF_TEXT,strName,0,0,0,0);
m_list01.SetItem(nitem,2,LVIF_TEXT,strAge,0,0,0,0);
rc->MoveNext();
}
rc->Close();
delete rc;
}
}