gusucode.com > 一个相对很完善的数据挖掘系统源码程序 > 一个相对很完善的数据挖掘系统源码程序/Discover/TreeViewDataManage.cpp

    // TreeViewDataManage.cpp : implementation file
//

#include "stdafx.h"
#include "Discover.h"
#include "TreeViewDataManage.h"
#include "MainFrm.h"
#include "DummyList.h"
#include "colledoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTreeViewDataManage

IMPLEMENT_DYNCREATE(CTreeViewDataManage, CTreeView)

CTreeViewDataManage::CTreeViewDataManage()
{
}

CTreeViewDataManage::~CTreeViewDataManage()
{
//		CTreeViewDataManage* TreeViewDataManage=new CTreeViewDataManage(); 
//	TreeViewDataManage=(CTreeViewDataManage*)m_wndWorkspace.GetView(RUNTIME_CLASS(CTreeViewDataManage));
//	TreeViewDataManagem_pRecordset->Close();///关闭记录集
//     TreeViewDataManagem_pConnection->Close();///关闭连接
// 	 delete TreeViewDataManage;
//	  m_pRecordset->Close();///关闭记录集
//       m_pConnection->Close();///关闭连接
}


BEGIN_MESSAGE_MAP(CTreeViewDataManage, CTreeView)
	//{{AFX_MSG_MAP(CTreeViewDataManage)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
//	ON_UPDATE_COMMAND_UI(ID_INDICATOR_RECORDCOUNT, OnUpdateIndicatorRECORDCOUNT)
// 	ON_UPDATE_COMMAND_UI(ID_INDICATOR_RECORDROWID, OnUpdateIndicatorRECORDROWID)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeViewDataManage drawing

void CTreeViewDataManage::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CTreeViewDataManage diagnostics

#ifdef _DEBUG
void CTreeViewDataManage::AssertValid() const
{
	CTreeView::AssertValid();
}

void CTreeViewDataManage::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTreeViewDataManage message handlers

void CTreeViewDataManage::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CTreeViewDataManage::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

static CString csTree[] =
{
	_T("Data Manage"),
	_T("Dataset"),
	_T("Partition Dataset"),
	_T("Sampling"),
	_T("Missing Data")
};

int CTreeViewDataManage::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CTreeView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	m_TreeCtrl = &GetTreeCtrl();

	// Create the image list for the tree control
	m_ImageList.Create (IDB_IL_CLASS, 16, 1, RGB(0,255,0));
	m_TreeCtrl->SetImageList (&m_ImageList, TVSIL_NORMAL);

	// Add the parent item
	HTREEITEM htiParent = m_TreeCtrl->InsertItem (csTree[0]);
	HTREEITEM htiChild; // child item
	HTREEITEM htiThird; // Third layer item
	HTREEITEM htiForth; // Forth layer item
	m_TreeCtrl->SetItemState( htiParent, TVIS_BOLD, TVIS_BOLD );

	// Add Tree children
		htiChild =m_TreeCtrl->InsertItem (csTree[1], 0, 1, htiParent, TVI_LAST);
		m_TreeCtrl->SetItemState (htiChild, TVIS_BOLD, TVIS_BOLD);
		htiThird=m_TreeCtrl->InsertItem ("MS Excel .xls", 1, 2, htiChild, TVI_LAST);
			m_TreeCtrl->InsertItem ("BostonHousing", 2, 3, htiThird, TVI_LAST);
			m_TreeCtrl->InsertItem ("TransactionDB", 2, 3, htiThird, TVI_LAST);
			m_TreeCtrl->InsertItem ("SQL2Exl", 2, 3, htiThird, TVI_LAST);
		htiThird=m_TreeCtrl->InsertItem ("MS Access .mdb", 1, 2, htiChild, TVI_LAST);
			htiForth=m_TreeCtrl->InsertItem ("Transaction", 2, 3, htiThird, TVI_LAST);
				m_TreeCtrl->InsertItem ("BostonHousing", 3, 0, htiForth, TVI_LAST);
				m_TreeCtrl->InsertItem ("TransactionDB", 3, 0, htiForth, TVI_LAST);
		htiThird=m_TreeCtrl->InsertItem ("SQL Server", 1, 2, htiChild, TVI_LAST);
			m_TreeCtrl->InsertItem ("trkscrsl", 2, 3, htiThird, TVI_LAST);
		htiThird=m_TreeCtrl->InsertItem ("TEXT", 1, 2, htiChild, TVI_LAST);
			m_TreeCtrl->InsertItem ("BostonHousing.csv", 2, 3, htiThird, TVI_LAST);
			m_TreeCtrl->InsertItem ("TransactionDB.txt", 2, 3, htiThird, TVI_LAST);
			m_TreeCtrl->InsertItem ("ID3DB.txt", 2, 3, htiThird, TVI_LAST);

	// Add children
	m_TreeCtrl->InsertItem (csTree[2], 2, 1, htiParent, TVI_LAST);
	m_TreeCtrl->InsertItem (csTree[3], 2, 1, htiParent, TVI_LAST);
	m_TreeCtrl->InsertItem (csTree[4], 2, 1, htiParent, TVI_LAST);
	m_TreeCtrl->Expand(htiParent, TVE_EXPAND);

	return 0;
}

BOOL CTreeViewDataManage::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
	return CTreeView::PreCreateWindow(cs);
}

void CTreeViewDataManage::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_TreeCtrl=&(this->GetTreeCtrl());
	UINT m_uFlags;
	HTREEITEM hItem =m_TreeCtrl->HitTest(point, &m_uFlags);
	//if ( (m_uFlags&TVHT_ONITEMLABEL ))用此语句只能在单击Item的文字时有效,单击到Item前面的图标时无效
	//if ( (m_uFlags&TVHT_ONITEM))
	if ((hItem != NULL) && (m_uFlags&TVHT_ONITEM))
	{
		m_TreeCtrl->Select(hItem,TVGN_CARET);//上面HitTest语句定位到TreeItem,本句选中该Item
		CString sTreeLabel=m_TreeCtrl->GetItemText(hItem);
		_bstr_t sConnection;
		//AfxMessageBox(sTreeLabel);
		HRESULT hr;//数据库操作返回值
		_variant_t RecordsAffected;
		_variant_t vCRIM,vZN,vINDUS,vCHAS;//数据库表各个字段对应的变量
		BeginWaitCursor();
		try
		{
			//CDiscoverApp *theApp=(CDiscoverApp *)AfxGetApp();
			m_pConnection=theApp.m_pConnection;
			m_pRecordset=theApp.m_pRecordset;
			if (m_pRecordset->State==adStateOpen) m_pRecordset->Close();
			if (m_pConnection->State==adStateOpen) m_pConnection->Close();
		
//			hr =m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
// 			m_pRecordset.CreateInstance("ADODB.Recordset");
			
		//	if(SUCCEEDED(hr))
		//	{
				
				if(	m_TreeCtrl->GetItemText(m_TreeCtrl->GetParentItem(hItem))=="MS Excel .xls" )
				{
					sConnection=(_bstr_t)("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq="+sTreeLabel+".xls");
					//sConnection=(_bstr_t)("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+sTreeLabel+".xls;"+"Extended Properties=;Excel 8.0;HDR=Yes");
					hr = m_pConnection->Open(sConnection ,"","",adModeUnknown);
				}
				else if (m_TreeCtrl->GetItemText(m_TreeCtrl->GetParentItem(m_TreeCtrl->GetParentItem(hItem)))=="MS Access .mdb" )
				{
					sConnection=(_bstr_t)("Driver={Microsoft Access Driver (*.mdb)};Dbq="+m_TreeCtrl->GetItemText(m_TreeCtrl->GetParentItem(hItem))+".mdb;");
					//上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;
					//hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=transaction.mdb","","",adModeUnknown);///连接数据库
					hr = m_pConnection->Open(sConnection ,"","",adModeUnknown);
				}
				else if (m_TreeCtrl->GetItemText(m_TreeCtrl->GetParentItem(hItem))=="TEXT" )
				{
					sConnection=(_bstr_t)("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=;Extensions=asc,csv,tab,txt;Persist Security Info=False");
					hr = m_pConnection->Open(sConnection ,"","",adModeUnknown);
				}	
				else if (m_TreeCtrl->GetItemText(m_TreeCtrl->GetParentItem(hItem))=="SQL Server" )
				{
					sConnection=(_bstr_t)("Driver={SQL Server};Server=CONFMGR;Database=cbps8jn;Uid=cbps8usr;Pwd=cbps8usrcp");
					hr = m_pConnection->Open(sConnection ,"","",adModeUnknown);
				}
				
				else return;
		//	}
				
//				m_pRecordset =  m_pConnection->Execute((_bstr_t)("SELECT COUNT(*) FROM "+sTreeLabel),&RecordsAffected,adCmdText);
//				_variant_t vIndex = (long)0;
//				_variant_t vCount = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
// 				m_pRecordset->Close();///关闭记录集
//				CString message;
//				message.Format("共有%d条记录",vCount.lVal);
// 				AfxMessageBox(message);///显示当前记录条数
				

				m_pRecordset->Open((_variant_t)("SELECT * FROM "+sTreeLabel),_variant_t((IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
				sTableName=sTreeLabel;
				theApp.sTableName=sTableName;
				theApp.sSQL="SELECT * FROM "+sTreeLabel;
				CMainFrame* appMainWnd=(CMainFrame*)AfxGetMainWnd();//得到主框架指针
				CDummyList* listInPane=(CDummyList*)appMainWnd->m_wndSplitter2.GetPane(0,0);//得到CDummyList对象的指针
				CListCtrl& lc=listInPane-> GetListCtrl();//得到列表控件CListCtrl的指针

				lc.DeleteAllItems();
				//lc.SetCallbackMask( LVIS_OVERLAYMASK    );

				int nCount = lc.GetHeaderCtrl()->GetItemCount();
				for (int it=0;it < nCount;it++)
				{
					lc.DeleteColumn(0);
 				}

				FieldsPtr myFieldsPtr;
				myFieldsPtr=m_pRecordset->GetFields( );
				FieldPtr myFieldPtr;
				_bstr_t FiledName;
				 int FiledsCount=myFieldsPtr->Count;
				//_variant_t FiledValue[15];
				CStringArray FiledValue;
				FiledValue.SetSize(FiledsCount);
				long ml;
				for(ml=0;ml<FiledsCount;ml++)
				{
//					if(FiledsCount>50)
// 						FiledsCount=30;
					myFieldPtr=myFieldsPtr->GetItem(ml);
					FiledName=myFieldPtr->GetName();
  					//MessageBox((LPCSTR)FiledName);
					lc.InsertColumn(ml,(LPCSTR)FiledName,LVCFMT_LEFT,90);
				}
				m_pRecordset->MoveFirst();
				long i=0;
				while(!m_pRecordset->adoEOF)//循环处理所有记录
				{
					vCRIM = m_pRecordset->GetCollect(_variant_t((long)0));///取得第1列的值,从0开始计数,也可以直接给出列的名称,如下一行
					//	vZN = m_pRecordset->GetCollect("ZN");///取得ZN字段的值
					lc.InsertItem(i,(LPCTSTR)(_bstr_t)vCRIM);
					for(ml=0;ml<FiledsCount-1;ml++)
					{
						//FiledValue[ml+1]=m_pRecordset->GetCollect(_variant_t((long)(ml+1)));
						FiledValue.InsertAt(ml,(LPCTSTR)(_bstr_t)(m_pRecordset->GetCollect(_variant_t((long)(ml+1)))));
						//if(FiledValue[ml+1].vt != VT_NULL)
						if(FiledValue.GetAt(ml) != VT_NULL)
						{
							//lc.SetItemText(i,ml+1,(LPCTSTR)(_bstr_t)FiledValue[ml+1]);
							lc.SetItemText(i,ml+1,(CString)FiledValue.GetAt(ml));
							//MessageBox(FiledValue[ml]);
						}
					}
				
					m_pRecordset->MoveNext();///移到下一条记录//
					i++;
					if(FiledsCount*i>=20000) {//数据量过大,提前终止填充列表控件,防止死机
						CMainFrame* appMainWnd=(CMainFrame*)AfxGetMainWnd();//得到主框架指针
						appMainWnd->m_wndStatusBar.SetPaneText(0,"! Not all Records been listed");//设置第一个pane内的文字
						break;
					}

				}
				theApp.m_pConnection=m_pConnection;
				theApp.m_pRecordset=m_pRecordset;
				//m_pRecordset->Close();///关闭记录集
 				//m_pConnection->Close();///关闭连接	
			}
			catch(_com_error e)///捕捉异常
			{
				CString errormessage;
				errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
				AfxMessageBox(errormessage);///显示错误信息
				if (m_pRecordset->State!=adStateClosed) m_pRecordset->Close();
				if (m_pConnection->State!=adStateClosed) m_pConnection->Close();
				
	CMainFrame* appMainWnd=(CMainFrame*)AfxGetMainWnd();//得到主框架指针
	appMainWnd->m_wndWorkspace.SetActiveView(RUNTIME_CLASS(CTreeViewDataManage));//更新view,防止数据库连接失败时的view无法显示完全

			}
			EndWaitCursor();
			//this->UpdateWindow();
			
	}


	CMainFrame* appMainWnd=(CMainFrame*)AfxGetMainWnd();//得到主框架指针
	//appMainWnd->m_wndStatusBar.SetPaneText(0,"This is an apple");
	appMainWnd->m_wndStatusBar.UpdateWindow();
	//appMainWnd->m_wndWorkspace.SetActiveView(RUNTIME_CLASS(CTreeViewDataManage));
	CTreeView::OnLButtonDown(nFlags, point);
}

void CTreeViewDataManage::OnDestroy() 
{
CTreeView::OnDestroy();
 	
}