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

📄 dagdetialview.cpp

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

#include "stdafx.h"
#include "dag.h"
#include "DagDetialView.h"
#include "DagView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//-------------------------
extern CDagApp theApp; 
/////////////////////////////////////////////////////////////////////////////
// CDagDetialView

IMPLEMENT_DYNCREATE(CDagDetialView, CListView)

CDagDetialView::CDagDetialView()
{
	m_strTableName="sambios010101";
}

CDagDetialView::~CDagDetialView()
{
}


BEGIN_MESSAGE_MAP(CDagDetialView, CListView)
	//{{AFX_MSG_MAP(CDagDetialView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDagDetialView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CDagDetialView diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CDagDetialView message handlers

void CDagDetialView::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);
}

void CDagDetialView::FillData(CListCtrl& refListCtrl)
{
	CMainFrame* pFrame=static_cast<CMainFrame*>(AfxGetMainWnd());
	CDagView* pDagView=static_cast<CDagView*>(pFrame->m_wndSplitter2.GetPane(0,0));
//	VERIFY(pDagView!=NULL);   
    refListCtrl.DeleteAllItems();
    int nColumnCount = refListCtrl.GetHeaderCtrl()->GetItemCount();
	
	// Delete all of the columns.
	for (int i=0;i < nColumnCount;i++)
	{
		refListCtrl.DeleteColumn(0);
	}
	//build SQL statement
	CString filter;
    for(i=0;i<pDagView->m_pkeys.GetSize();i++)
	{
		CString strValue,key;
		key=(LPCTSTR)pDagView->m_pkeys.GetAt(i);
		pDagView->m_pkeysMap.Lookup((LPCTSTR)pDagView->m_pkeys.GetAt(i),strValue);
		filter+=key+"='"+strValue+"' and ";
	}
	if(filter!="")
		filter=filter.Left(filter.GetLength()-4);
    CString strSql;
	filter.TrimLeft();
	if(filter!="")
		strSql="select * from "+m_strTableName+" where "+filter;
	else
		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;
	ColumnList.RemoveAll();
	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();
	}


}

⌨️ 快捷键说明

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