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

📄 dagview.cpp

📁 VC操作SQL Server主从表,一种利用VC操作数据库主从表的源码
💻 CPP
字号:
// dagView.cpp : implementation of the CDagView class
//

#include "stdafx.h"
#include "dag.h"
#include <afxtempl.h>
#include "dagDoc.h"
#include "dagView.h"
#include "mainfrm.h"
#include "dagDetialView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//-------------------------
extern CDagApp theApp; 
/////////////////////////////////////////////////////////////////////////////
// CDagView

IMPLEMENT_DYNCREATE(CDagView, CListView)

BEGIN_MESSAGE_MAP(CDagView, CListView)
	//{{AFX_MSG_MAP(CDagView)
	ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDagView construction/destruction

CDagView::CDagView()
{
	m_strTableName="sambios0101";
}

CDagView::~CDagView()
{
}

BOOL CDagView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDagView drawing

void CDagView::OnDraw(CDC* pDC)
{
	CDagDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}

void CDagView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	CListCtrl& refListCtrl=GetListCtrl();
	long dwStyleOld;
    dwStyleOld = GetWindowLong(refListCtrl.m_hWnd, GWL_STYLE);
	dwStyleOld |= LVS_REPORT|LVS_SHOWSELALWAYS;
	SetWindowLong(refListCtrl.GetSafeHwnd(), GWL_STYLE,dwStyleOld);
    dwStyleOld=refListCtrl.GetExtendedStyle();
    dwStyleOld |= LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT;
	refListCtrl.SetExtendedStyle(dwStyleOld);
    FillData(refListCtrl);	
	
}

/////////////////////////////////////////////////////////////////////////////
// CDagView printing

BOOL CDagView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDagView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

void CDagView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

/////////////////////////////////////////////////////////////////////////////
// CDagView diagnostics

#ifdef _DEBUG
void CDagView::AssertValid() const
{
	CListView::AssertValid();
}

void CDagView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CDagDoc* CDagView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDagDoc)));
	return (CDagDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDagView message handlers

void CDagView::FillData(CListCtrl& refListCtrl)
{

	refListCtrl.DeleteAllItems();
	
	int nColumnCount = refListCtrl.GetHeaderCtrl()->GetItemCount();
	
	// Delete all of the columns.
	for (int i=0;i < nColumnCount;i++)
	{
		refListCtrl.DeleteColumn(0);
	}
	CString strSql="select * from "+m_strTableName;
	_RecordsetPtr pRS;
	FieldsPtr  fldLoop = NULL;
	_variant_t Index;
	Index.vt=VT_I2;
	pRS.CreateInstance(__uuidof(Recordset));
	try
	{
		pRS->Open((LPCTSTR)strSql,                // 查询表中所有字段
							theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
							adOpenDynamic,
							adLockOptimistic,
							adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	fldLoop = pRS->GetFields();
	CArray<_bstr_t,_bstr_t> ColumnList;
	for (i = 0; i < (int)fldLoop->GetCount(); i++)
	{
		Index.iVal=i;
		_bstr_t ColName=fldLoop->GetItem(Index)->GetName();
		ColumnList.Add(ColName);
		refListCtrl.InsertColumn(i,(LPSTR)ColName,LVCFMT_LEFT,50);
		// (int)fldLoop->GetItem(Index)->GetAttributes());
	}
	if(!pRS->BOF)
	pRS->MoveFirst();
	_variant_t var;
	CString strValue;
	i=0;	
	while(!pRS->adoEOF)
	{
		refListCtrl.InsertItem(i,"");
        for(int j=0;j<ColumnList.GetSize();j++)
		{
			
			_bstr_t Col=ColumnList.GetAt(j);					
			var=pRS->GetCollect((const _variant_t)(Col));
			if(var.vt != VT_NULL)
				strValue = (LPCSTR)_bstr_t(var);
			
			refListCtrl.SetItemText(i,j,strValue);
	
		}	
		i++;
		pRS->MoveNext();
	}

}

int CDagView::GetColumnIndex(CListCtrl &ref,LPCTSTR strCol)
{
	CHeaderCtrl * pHeader=ref.GetHeaderCtrl();
	int nCount=pHeader->GetItemCount();
	TCHAR  lpBuffer[256];
	bool   fFound = false;
	HDITEM hdi;
	hdi.mask = HDI_TEXT;
	hdi.pszText = lpBuffer;
	hdi.cchTextMax = 256;
	int index=-1;
	for (int i=0;!fFound && (i < nCount);i++)
	{
		pHeader->GetItem(i, &hdi);
		
		if (strcmp(hdi.pszText, strCol) == 0)
		{
			index=i;
			fFound = true;
		}
	}
	return index;
}

void CDagView::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	CListCtrl& refListCtrl=GetListCtrl();
	if(pNMListView->uNewState&LVIS_SELECTED)
	{
		m_pkeys.RemoveAll();
		m_pkeysMap.RemoveAll();
		int iCurrent = pNMListView->iItem;
		_RecordsetPtr pRSKeys;
		pRSKeys.CreateInstance(__uuidof(Recordset));
		CString strSql="sp_pkeys "+m_strTableName;
		try
		{
			pRSKeys->Open((LPCTSTR)strSql,                // 查询表中所有字段
				theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
				adOpenDynamic,
				adLockOptimistic,
				adCmdText);
			
		}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		if(!pRSKeys->BOF)
		pRSKeys->MoveFirst();
		_variant_t var;
		while(!pRSKeys->adoEOF)
		{
			var=pRSKeys->GetCollect("COLUMN_NAME");
			if(var.vt != VT_NULL)
			{
				m_pkeys.Add(_bstr_t(var));
			    int iSubItem=GetColumnIndex(refListCtrl,(LPSTR)_bstr_t(var));
				CString strValue=refListCtrl.GetItemText(iCurrent,iSubItem);
				m_pkeysMap.SetAt(_bstr_t(var),strValue);
			}
			pRSKeys->MoveNext();
		}
	}
	UpdateDetialView();
	*pResult = 0;
}

void CDagView::UpdateDetialView()
{
	CMainFrame* pFrame=static_cast<CMainFrame*>(AfxGetMainWnd());
	CDagDetialView* pDagDetialView=static_cast<CDagDetialView*>(pFrame->m_wndSplitter2.GetPane(1,0));
	pDagDetialView->OnInitialUpdate();

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -