?? odbclistview.cpp
字號:
// ODBCListView.cpp : implementation of the CODBCListView class
//
#include "stdafx.h"
#include "ODBCDB.h"
//OOOO>
//#include "ODBCListDoc.h"
//<OOOO
#include "ODBCListView.h"
//●》
#include "ODBCINST.H"
//《●
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CODBCListView
IMPLEMENT_DYNCREATE(CODBCListView,CListView)
BEGIN_MESSAGE_MAP(CODBCListView,CListView)
//{{AFX_MSG_MAP(CODBCListView)
ON_WM_LBUTTONDBLCLK()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CODBCListView construction/destruction
CODBCListView::CODBCListView()
{
// TODO: add construction code here
m_set=NULL;
m_List=&GetListCtrl();
m_dwDefaultStyle |= LVS_REPORT;
//●>
m_NKey=0;
m_DSOK=TRUE;
//<●
}
CODBCListView::~CODBCListView()
{
delete m_set;
}
BOOL CODBCListView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CODBCListView drawing
void CODBCListView::OnDraw(CDC* pDC)
{
//OOOO>
// CODBCListDoc* pDoc = GetDocument();
// ASSERT_VALID(pDoc);
//<OOOO
// TODO: add draw code for native data here
}
void CODBCListView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
//★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
//●>
if(m_set==NULL)
{
if(!SQLConfigDataSource
(
NULL, //父窗口指針;
ODBC_ADD_DSN, //請求的類型;
"Microsoft Access Driver (*.mdb)",//驅動程序名;
//屬性:
"DSN=Company\0" //數據源名稱;
"Description=This a sample\0" //數據源的說明;
"FileType=Microsoft Access\0" //數據源文件類型說明;
"DBQ=F:\\Myfiles\\AVCB\\Initial\\Company.mdb\0"//數據源文件全路徑名;
"MaxScanRows=8\0" //在根據現有數據設置列的數據類型時
//所要掃描的行數。 可以為 1 到 16,
//默認值為 8;如果設置為 0,將掃描
//所有行。(如果數字超出界限,會返
//回一個錯誤)
)
)
{
AfxMessageBox("無法創建數據源!");
m_DSOK=FALSE;
return;
}
m_set=new CRecordset(&m_Db);
if (!m_Db.OpenEx(_T("DSN=Company"),0))
{
AfxMessageBox("你選擇了取消");
return;
}
m_set->Open( CRecordset::dynaset,
_T( "Select * from Basic" ));
}
//1. 添加列:
short i,j;
short nFields;
CODBCFieldInfo Fi;
CString tmp;
LV_COLUMN lc;
lc.mask = LVCF_FMT | LVCF_WIDTH |
LVCF_TEXT | LVCF_SUBITEM;
nFields=m_set->GetODBCFieldCount();
for(i=0;i<nFields;i++)
{
m_set->GetODBCFieldInfo(i,Fi);
lc.iSubItem = i;
lc.fmt = LVCFMT_CENTER;
lc.cx = Fi.m_strName.GetLength()*10+16;
lc.pszText=Fi.m_strName.GetBuffer(Fi.m_strName.GetLength());
GetListCtrl().InsertColumn(i, &lc);
}
//2. 添加行:
LV_ITEM Item;
CString str;
CDBVariant var;
i=0;
if(m_set->IsEOF())
{
AfxMessageBox("當前視圖沒有記錄!");
return;
}
m_set->MoveFirst();
while(m_set->IsEOF()==0)
{
Item.mask=LVIF_TEXT;
//第i 行:
Item.iItem=i;
//第一列:
Item.iSubItem=0;
m_set->GetFieldValue((short)0,str);
Item.pszText=str.GetBuffer(str.GetLength());
m_List->InsertItem(&Item);
//其它各列:
for(j=1;j<nFields;j++)
{
//==>Wrong!
m_set->GetFieldValue((short)j,str);//獲取數據;
m_List->SetItemText(i,j,str.GetBuffer(str.GetLength()));
}
m_set->MoveNext();
i++;//計數器;
}
//<●
//★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
}
/////////////////////////////////////////////////////////////////////////////
// CODBCListView printing
BOOL CODBCListView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CODBCListView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CODBCListView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CODBCListView diagnostics
#ifdef _DEBUG
void CODBCListView::AssertValid() const
{
CListView::AssertValid();
}
void CODBCListView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
//●>
/*
CODBCListDoc* CODBCListView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CODBCListDoc)));
return (CODBCListDoc*)m_pDocument;
}
*/
//<●
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CODBCListView message handlers
void CODBCListView::SetRecordset(CRecordset *pSet,UINT nk)
{
m_set=pSet;
m_NKey=nk;
}
void CODBCListView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CListView::OnLButtonDblClk(nFlags, point);
m_CurSel=m_List->GetSelectionMark();
m_set->MoveFirst();
if(m_CurSel!=-1)
{
m_set->Move(m_CurSel);
}
m_set->GetFieldValue(m_NKey,m_Key);
AfxMessageBox("當前記錄的指定字段取值為:"+m_Key);
}
void CODBCListView::OnDestroy()
{
CListView::OnDestroy();
// TODO: Add your message handler code here
//●>
if(m_DSOK==FALSE)
{
return;
}
if(m_Db.IsOpen())
{
m_Db.Close();
}
if(m_set->IsOpen())
{
m_set->Close();
}
//<●
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -