📄 ado1view.cpp
字号:
// ado1View.cpp : implementation of the CAdo1View class
//
#include "stdafx.h"
#include "ado1.h"
#include "ado1Doc.h"
#include "ado1View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include"mydlg.h"
/////////////////////////////////////////////////////////////////////////////
// CAdo1View
IMPLEMENT_DYNCREATE(CAdo1View, CListView)
BEGIN_MESSAGE_MAP(CAdo1View, CListView)
//{{AFX_MSG_MAP(CAdo1View)
ON_WM_DESTROY()
ON_COMMAND(ID_OPENDATE, OnOpendate)
ON_COMMAND(ID_modify, Onmodify)
ON_COMMAND(ID_DELETEDATE, OnDeletedate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdo1View construction/destruction
CAdo1View::CAdo1View()
{
// TODO: add construction code here
}
CAdo1View::~CAdo1View()
{
}
BOOL CAdo1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style|=LVS_REPORT;
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CAdo1View drawing
void CAdo1View::OnDraw(CDC* pDC)
{
CAdo1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CAdo1View::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl &m_listctrl=this->GetListCtrl();
CString string[]={"学号","成绩","班级"};
for(int i=0;i<3;i++)
{
m_listctrl.InsertColumn(i,string[i],LVCFMT_LEFT,60);
}
::CoInitialize(NULL);
this->m_pconnection.CreateInstance(_uuidof(Connection));
this->m_precordset.CreateInstance(_uuidof(Recordset));
//this->m_pconnection.CreateInstance(_uuidof(Connection));
this->m_pconnection->ConnectionString="File Name=11.udl";
m_pconnection->Open("","","",0);
// m_pconnection->Open("DSN=datebasend","","",0);
this->intinstance();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
}
/////////////////////////////////////////////////////////////////////////////
// CAdo1View diagnostics
#ifdef _DEBUG
void CAdo1View::AssertValid() const
{
CListView::AssertValid();
}
void CAdo1View::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CAdo1Doc* CAdo1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAdo1Doc)));
return (CAdo1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAdo1View message handlers
void CAdo1View::OnDestroy()
{
CListView::OnDestroy();
if(this->m_pconnection)
{
this->m_pconnection->Close();
}
// TODO: Add your message handler code here
}
void CAdo1View::intinstance()
{
CListCtrl&m_listctrl=this->GetListCtrl();
m_listctrl.DeleteAllItems();
int n=0;
CString strsql="SELECT*FROM course ";
BSTR bstrsql=strsql.AllocSysString();//转化成BSTR形的字符串
this->m_precordset->Open(bstrsql,(IDispatch*)m_pconnection,adOpenDynamic,adLockOptimistic,
adCmdText);
while(!this->m_precordset->adoEOF)
{
::_variant_t value;
value=this->m_precordset->GetCollect("stuno");
m_listctrl.InsertItem(n,(char*)_bstr_t(value));
value=this->m_precordset->GetCollect("course");
m_listctrl.SetItemText(n,1,(char*)_bstr_t(value));
this->m_precordset->MoveNext();
n++;
}
this->m_precordset->Close();
}
void CAdo1View::OnOpendate()
{
Cmydlg dlg;
// dlg.UpdateData();///没有想通为什么不要这一个函数.
if(dlg.DoModal()!=IDOK)
{
return;
}
CString strsql="SELECT*FROM course ";
BSTR bstrsql=strsql.AllocSysString();//转化成BSTR形的字符串
this->m_precordset->Open(bstrsql,(IDispatch*)m_pconnection,adOpenDynamic,adLockOptimistic,
adCmdText);
this->m_precordset->AddNew();
this->m_precordset->PutCollect("stuno",_variant_t(dlg.m_stuno));
this->m_precordset->PutCollect("course",_variant_t(dlg.m_course));
this->m_precordset->Update();
this->m_precordset->Close();
this->intinstance();
// TODO: Add your command handler code here
}
void CAdo1View::Onmodify()
{
Cmydlg dlg;
CListCtrl&m_listctrl=this->GetListCtrl();
POSITION pos;
pos=m_listctrl.GetFirstSelectedItemPosition();
if(pos==NULL)
{
this->MessageBox("you didn't select any item");
return;
}
int Item=m_listctrl.GetNextSelectedItem(pos);
CString string0,string1;
string0=m_listctrl.GetItemText(Item,0);
string1=m_listctrl.GetItemText(Item,1);
CString strsql="SELECT*FROM course ";
BSTR bstrsql=strsql.AllocSysString();//转化成BSTR形的字符串
this->m_precordset->Open(bstrsql,(IDispatch*)m_pconnection,adOpenDynamic,adLockOptimistic,
adCmdText);
long tt=atoi(string0);
long ss=atoi(string1);
dlg.m_course=ss;
dlg.m_stuno=tt;
//上面是把数据显示在对话框里
//下面是修改.
if(dlg.DoModal()==IDC_BUTTON_modify)
{
return;
}
this->m_precordset->PutCollect("stuno",_variant_t(dlg.m_stuno));
this->m_precordset->PutCollect("course",_variant_t(dlg.m_course));
this->m_precordset->Update();
this->m_precordset->Close();
this->intinstance();
// _variant_t value;
// value=this->m_precordset->GetCollect("stuno");
//dlg.m_stuno
// TODO: Add your command handler code here
}
void CAdo1View::OnDeletedate()
{
CListCtrl &m_listctrl=this->GetListCtrl();
POSITION pos;
pos=m_listctrl.GetFirstSelectedItemPosition();
if(pos==NULL)
{
this->MessageBox("u didn't select item");
return;
}
// int Item=m_listctrl.GetNextItem(
// TODO: Add your command handler code here
}
BOOL CAdo1View::ConnDB()
{
/* ::CoInitialize(NULL);
this->m_pconnection.CreateInstance(_uuidof(Connection));
this->m_precordset.CreateInstance(_uuidof(Recordset));
/ this->m_pconnection->ConnectionString="File Name=E:\001\homework\ado1\11.udl";
m_pconnection->Open("","","",0);
this->intinstance();*/
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -