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

📄 statquerydlg.cpp

📁 < Visual C+++SQL Server数据库开发与实例>>一书中的实例核心源程序提供了几个案例的编程源代码。
💻 CPP
字号:
// StatQueryDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CRDBS.h"
#include "StatQueryDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CStatQueryDlg dialog


CStatQueryDlg::CStatQueryDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStatQueryDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStatQueryDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CStatQueryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStatQueryDlg)
	DDX_Control(pDX, IDC_LIST_VOCATION, m_listVocation);
	DDX_Control(pDX, IDC_LIST_PROFESSION, m_listProfession);
	DDX_Control(pDX, IDC_LIST_AREA, m_listArea);
	DDX_Control(pDX, IDC_LIST_APPRAOCH, m_listApproach);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CStatQueryDlg, CDialog)
	//{{AFX_MSG_MAP(CStatQueryDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStatQueryDlg message handlers

BOOL CStatQueryDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES |
		LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE | LVS_EX_UNDERLINEHOT;
	m_listArea.SetExtendedStyle(dwExStyle);
	m_listApproach.SetExtendedStyle(dwExStyle);
	m_listProfession.SetExtendedStyle(dwExStyle);
	m_listVocation.SetExtendedStyle(dwExStyle);
	
	//初始地区统计信息列表框控件
	//添加地区名称列
	LV_COLUMN lvColumn;
	lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvColumn.fmt = LVCFMT_CENTER;
	lvColumn.cx = 90;
	lvColumn.iSubItem = 0;     
	lvColumn.pszText = "地区名称";
	m_listArea.InsertColumn(0, &lvColumn);
	//添加数目列
	lvColumn.cx = 50;  
	lvColumn.iSubItem = 1; 
	lvColumn.pszText = "数目";
	m_listArea.InsertColumn(1, &lvColumn);
	
	//初始结识途径统计信息列表框控件
	//添加结识途径名称列
	lvColumn.cx = 90;
	lvColumn.iSubItem = 0;     
	lvColumn.pszText = "结识途径名称";
	m_listApproach.InsertColumn(0, &lvColumn);
	//添加数目列
	lvColumn.cx = 50;  
	lvColumn.iSubItem = 1; 
	lvColumn.pszText = "数目";
	m_listApproach.InsertColumn(1, &lvColumn);
	
	//初始职位信息统计列表框控件
	//添加职位名称列
	lvColumn.cx = 90;
	lvColumn.iSubItem = 0;     
	lvColumn.pszText = "职位名称";
	m_listProfession.InsertColumn(0, &lvColumn);
	//添加数目列
	lvColumn.cx = 50;  
	lvColumn.iSubItem = 1; 
	lvColumn.pszText = "数目";
	m_listProfession.InsertColumn(1, &lvColumn);
	
	//初始行业信息统计列表框控件
	//添加行业名称列
	lvColumn.cx = 90;
	lvColumn.iSubItem = 0;     
	lvColumn.pszText = "行业名称";
	m_listVocation.InsertColumn(0, &lvColumn);
	//添加数目列
	lvColumn.cx = 50;  
	lvColumn.iSubItem = 1; 
	lvColumn.pszText = "数目";
	m_listVocation.InsertColumn(1, &lvColumn);
    //向列表框控件中添加数据
	InitListData();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CStatQueryDlg::InsertListItem(CListCtrl* pList, CString name, int value)
{
	//获取当前的纪录条数.
	int nIndex = pList->GetItemCount();
	LV_ITEM lvItem;
	lvItem.mask = LVIF_TEXT ;	
	lvItem.iItem = nIndex;				//行数
	lvItem.iSubItem = 0;
	lvItem.pszText = (char*)(LPCTSTR)name;		//第一列
	//在最后一行插入记录值.
	pList->InsertItem(&lvItem);	
	//设置该行的其他列的值.
	CString temp ;
	temp.Format("%d",value);	
	pList->SetItemText(nIndex,1,temp);
}

void CStatQueryDlg::InitListData()
{
	if(!m_pDB->IsOpen()){
		MessageBox("数据库未打开");
		return;
	}
	TRY{
		CRecordset rs(m_pDB);
		//打开所有的地区信息记录.
		rs.Open(CRecordset::dynaset, "select area,count(area) from customer_info_tab  group by area");
		while (!rs.IsEOF()) {
			int count;
			CString area;
			CDBVariant var;
			//获取地区名称字段值
			rs.GetFieldValue((short)0, area);		
			//获取统计数目字段值
			rs.GetFieldValue(1, var, SQL_C_SLONG);
			if (var.m_dwType != DBVT_NULL)	
				count = var.m_iVal;
			var.Clear();		
			InsertListItem(&m_listArea,area,count);
			rs.MoveNext();	
		}
		rs.Close();
		//打开所有的结识途径信息记录.
		rs.Open(CRecordset::dynaset, "select approach,count(approach) from customer_info_tab  group by approach");
		while (!rs.IsEOF()) {
			int count;
			CString approach;
			CDBVariant var;
			//获取结识途径字段值
			rs.GetFieldValue((short)0, approach);
			//获取统计数目字段值
			rs.GetFieldValue(1, var, SQL_C_SLONG);
			if (var.m_dwType != DBVT_NULL)	
				count = var.m_iVal;
			var.Clear();			
			InsertListItem(&m_listApproach,approach,count);
			rs.MoveNext();	
		}
		rs.Close();
		//打开所有的职位信息记录.
		rs.Open(CRecordset::dynaset, "select profession,count(profession) from customer_info_tab  group by profession");
		while (!rs.IsEOF()) {
			int count;
			CString profession;
			CDBVariant var;
			//获取职位名称字段值
			rs.GetFieldValue((short)0, profession);			
			//获取职位统计数目字段值
			rs.GetFieldValue(1, var, SQL_C_SLONG);
			if (var.m_dwType != DBVT_NULL)	
				count = var.m_iVal;
			var.Clear();			

			InsertListItem(&m_listProfession,profession,count);
			rs.MoveNext();	
		}
		rs.Close();
		//打开所有的行业信息记录.
		rs.Open(CRecordset::dynaset, "select vocation,count(vocation) from customer_info_tab  group by vocation");
		while (!rs.IsEOF()) {
			int count;
			CString vocation;
			CDBVariant var;
			//获取行业名称字段值
			rs.GetFieldValue((short)0, vocation);		
			//获取行业统计数目字段值
			rs.GetFieldValue(1, var, SQL_C_SLONG);
			if (var.m_dwType != DBVT_NULL)	
				count = var.m_iVal;
			var.Clear();			
	
			InsertListItem(&m_listVocation,vocation,count);
			rs.MoveNext();	
		}
		rs.Close();
	}
	CATCH(CDBException,ex)
	{
		AfxMessageBox (ex->m_strError);
		AfxMessageBox (ex->m_strStateNativeOrigin);		
	}
	AND_CATCH(CMemoryException,pEx)
	{
		pEx->ReportError();
		AfxMessageBox ("memory exception");
	}
	AND_CATCH(CException,e)
	{
		TCHAR szError[100];
		e->GetErrorMessage(szError,100);
		AfxMessageBox (szError);
	}
	END_CATCH   		
}

⌨️ 快捷键说明

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