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

📄 adoaccessdlg.cpp

📁 VisualC高级编程技术精粹.rar
💻 CPP
字号:
// ADOAccessDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ADOAccess.h"
#include "ADOAccessDlg.h"

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

extern CADOAccessApp theApp;   // 引用应用类中的theApp来获取库连接指针
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADOAccessDlg dialog

CADOAccessDlg::CADOAccessDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CADOAccessDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CADOAccessDlg)
	m_csName = _T("");
	m_csAge = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_csSex = _T("");
	m_nCurrentIndex = -1;
	m_nTotal = -1;
}

void CADOAccessDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CADOAccessDlg)
	DDX_Control(pDX, IDC_INFO_LIST, m_listctrlInfo);
	DDX_Control(pDX, IDC_COMBO_SEX, m_comboSex);
	DDX_Text(pDX, IDC_EDIT_NAME, m_csName);
	DDX_Text(pDX, IDC_EDIT_AGE, m_csAge);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CADOAccessDlg, CDialog)
	//{{AFX_MSG_MAP(CADOAccessDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
	ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
	ON_BN_CLICKED(IDC_BUTTON_QUIT, OnButtonQuit)
	ON_NOTIFY(NM_CLICK, IDC_INFO_LIST, OnClickInfoList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADOAccessDlg message handlers

BOOL CADOAccessDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	// 初始化下拉列表框
	m_comboSex.AddString(_T("男"));
	m_comboSex.AddString(_T("女"));
	m_comboSex.SetCurSel(0);

	// 初始化ListCtrl
	m_listctrlInfo.SetExtendedStyle( LVS_EX_GRIDLINES );
	m_listctrlInfo.SetExtendedStyle( m_listctrlInfo.GetExtendedStyle() | 
		                             LVS_EX_FULLROWSELECT );
	m_listctrlInfo.InsertColumn(0, "姓名", LVCFMT_CENTER, 200);
	m_listctrlInfo.InsertColumn(1, "性别", LVCFMT_CENTER, 150);
	m_listctrlInfo.InsertColumn(2, "年龄", LVCFMT_CENTER, 150);

	// 使用ADO创建数据库记录集
	m_pRecordset.CreateInstance(__uuidof(Recordset));

	// 打开记录集
	try
	{
		m_pRecordset->Open(_T("SELECT * FROM EmployeeTable"), // 查询EmployeeTable表中所有字段
							theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
							adOpenDynamic,
							adLockOptimistic,
							adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CADOAccessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CADOAccessDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CADOAccessDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CADOAccessDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	
	// 判决姓名是否为空
	if (m_csName.IsEmpty())
	{
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("姓名不能为空!"));
		return;
	}
	
	// 判决年龄是否为空
	if (m_csAge.IsEmpty())
	{
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("年龄不能为空!"));
		return;
	}

	// 获取性别
	int nCurSelSex = m_comboSex.GetCurSel();
    m_comboSex.GetLBText(nCurSelSex, m_csSex);

	// 在ADO操作中常用try...catch()来捕获错误信息,
	try
	{
		// 写入各字段值
		m_pRecordset->AddNew();
		m_pRecordset->PutCollect("姓名", _variant_t(m_csName));
		m_pRecordset->PutCollect("性别", _variant_t(m_csSex));
		m_pRecordset->PutCollect("年龄", atol(m_csAge));
		m_pRecordset->Update();

		// 显示添加成功的信息
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("添加数据成功!"));

		// 清除编辑框的内容
		ClearContent();			

		// 显示数据库中的所有记录
		OnButtonQuery();
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
}

void CADOAccessDlg::OnButtonDelete() 
{
	// TODO: Add your control notification handler code here
	try
	{
		// 判断列表是否为空
		if (m_listctrlInfo.GetItemCount() == 0)
		{
			GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("当前列表中没有数据!"));
			return;
		}
		// 取得第一条记录的位置
		POSITION pos = m_listctrlInfo.GetFirstSelectedItemPosition();
		if ( NULL == pos )
		{		
			// 若没有选择记录,则给出提示
			GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("你还没有选择一条记录,请选择!"));
			return;
		}		
		
		int nIndexInFieldList = -1;
		while (pos)
		{ 	
			// 取得被选择记录的索引
			nIndexInFieldList = m_listctrlInfo.GetNextSelectedItem(pos);

			// 高亮被的选择记录
			m_listctrlInfo.SetFocus();

			// 从文件中把当前行记录删除
			m_nTotal = m_listctrlInfo.GetItemCount();
			m_nCurrentIndex = ConvertIndex(nIndexInFieldList,m_nTotal - 1);
			m_pRecordset->MoveFirst();
			m_pRecordset->Move(long(m_nCurrentIndex));
			m_pRecordset->Delete(adAffectCurrent);
			m_pRecordset->Update();

			// 清除编辑框的内容
			ClearContent();
			
			// 删除被的选择记录
			m_listctrlInfo.DeleteItem( nIndexInFieldList );

			// 显示删除成功的信息
			GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("删除数据成功!"));

			// 再次取得第一条记录的位置
			POSITION pos = m_listctrlInfo.GetFirstSelectedItemPosition();

			// 设定被选中项目的状态
			UINT flag = LVIS_SELECTED|LVIS_FOCUSED;
			m_listctrlInfo.SetItemState( nIndexInFieldList, flag, flag );			
		}				

		// 显示数据库中的所有记录
		OnButtonQuery();
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
}

void CADOAccessDlg::OnButtonModify() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	// 判断列表是否为空
	if (m_listctrlInfo.GetItemCount() == 0)
	{
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("当前列表中没有数据!"));
		return;
	}
	
	// 判决姓名是否为空
	if (m_csName.IsEmpty())
	{
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("姓名不能为空!"));
		return;
	}
	
	// 判决年龄是否为空
	if (m_csAge.IsEmpty())
	{
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("年龄不能为空!"));
		return;
	}

	// 获取性别
	int nCurSelSex = m_comboSex.GetCurSel();
    m_comboSex.GetLBText(nCurSelSex, m_csSex);

	// 取得当前的位置
	POSITION pos = m_listctrlInfo.GetFirstSelectedItemPosition();
	if( NULL == pos )
	{		
		// 若没有选择记录,则给出提示
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("你还没有选择一条记录,请选择!"));
		return;
	}		
		
	int nIndexInFieldList = -1;
	while (pos)
	{ 	
		// 取得被选择记录的索引
		nIndexInFieldList = m_listctrlInfo.GetNextSelectedItem(pos);
	}
	
	// 取得列表中总的项目数
	m_nTotal = m_listctrlInfo.GetItemCount();

	// 转化当前选中项目的索引
	m_nCurrentIndex = ConvertIndex(nIndexInFieldList,m_nTotal - 1);

	// 在ADO操作常用try...catch()来捕获错误信息,
	try
	{
		// 写入字段值
		m_pRecordset->MoveFirst();
		m_pRecordset->Move(long(m_nCurrentIndex));
		m_pRecordset->Delete(adAffectCurrent);
		m_pRecordset->Update();
		//m_pRecordset->MoveLast();
		m_pRecordset->AddNew();
		m_pRecordset->PutCollect("姓名", _variant_t(m_csName));
		m_pRecordset->PutCollect("性别", _variant_t(m_csSex));
		m_pRecordset->PutCollect("年龄", atol(m_csAge));
		m_pRecordset->Update();		

		// 显示修改数据成功信息
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("修改数据成功!"));

		// 显示数据库中的所有记录
		OnButtonQuery();	
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}	
}

void CADOAccessDlg::OnButtonQuery() 
{
	// TODO: Add your control notification handler code here
	_variant_t var;
	CString csName = _T("");
	CString csSex = _T("");
	CString csAge = _T("");

	// 清空列表
	if (m_listctrlInfo.GetItemCount() !=0)
	{
		m_listctrlInfo.DeleteAllItems();
	}

	// 在ADO操作中常用try...catch()来捕获错误信息,
	try
	{
		if (!m_pRecordset->BOF)
		{
			m_pRecordset->MoveFirst();
		}
		else
		{
			GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("数据库表中没有数据!"));
			return;
		}

		// 读入库中各字段并加入列表中
		while (!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect("姓名");
			if (var.vt != VT_NULL)
			{
				csName = (LPCSTR)_bstr_t(var);
			}

			var = m_pRecordset->GetCollect("性别");
			if (var.vt != VT_NULL)
			{
				csSex = (LPCSTR)_bstr_t(var);
			}

			var = m_pRecordset->GetCollect("年龄");
			if (var.vt != VT_NULL)
			{
				csAge = (LPCSTR)_bstr_t(var);
			}
			
			// 添加数据到列表
			m_listctrlInfo.InsertItem(0, _T(""));
			m_listctrlInfo.SetItemText(0, 0, csName);
			m_listctrlInfo.SetItemText(0, 1, csSex);
			m_listctrlInfo.SetItemText(0, 2, csAge);			

			// 移到下一条记录
			m_pRecordset->MoveNext();		
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
}

void CADOAccessDlg::OnButtonQuit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void CADOAccessDlg::OnClickInfoList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	// 判断列表是否为空
	if (m_listctrlInfo.GetItemCount() == 0)
	{
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("当前列表中没有数据!"));
		return;
	}

	// 取得第一条记录的位置
	POSITION pos = m_listctrlInfo.GetFirstSelectedItemPosition();
	if ( NULL == pos )
	{		
		// 若没有选择记录,则给出提示
		GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("你还没有选择一条记录,请选择!"));
		return;
	}		

	while (pos)
	{ 	
		// 取得被选择记录的索引
		int nIndexInFieldList = m_listctrlInfo.GetNextSelectedItem(pos);

		// 高亮被的选择记录
		m_listctrlInfo.SetFocus();
		
		// 取得记录中的子项
		m_csName = m_listctrlInfo.GetItemText(nIndexInFieldList,0);
		m_csSex = m_listctrlInfo.GetItemText(nIndexInFieldList,1);
		m_csAge = m_listctrlInfo.GetItemText(nIndexInFieldList,2);
	}
	
	m_comboSex.SetWindowText(m_csSex);
	UpdateData(FALSE);

	*pResult = 0;
}

BOOL CADOAccessDlg::DestroyWindow() 
{
	// 关闭记录集
	m_pRecordset->Close();
	m_pRecordset = NULL;
	
	return CDialog::DestroyWindow();
}

int CADOAccessDlg::ConvertIndex(int nCurrent,int nTotal)
{
	int nTemp = -1;

	for (int i = nCurrent; i <= nTotal; i++)
	{
		nTemp = nCurrent;
		nCurrent = nTotal - i;
		nTotal = nTemp;
	}

	return nCurrent;
}

void CADOAccessDlg::ClearContent()
{
	m_csName = _T(""); 
	m_csAge = _T("");
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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