⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 odbclistview.cpp

📁 VC++高级编程技巧与示例
💻 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 + -