www.gusucode.com > VC++使用ADO在数据库中修改记录源码程序 > VC++使用ADO在数据库中修改记录源码程序\code\DemoDlg.cpp
// DemoDlg.cpp : implementation file // Download by http://www.NewXing.com #include "stdafx.h" #include "Demo.h" #include "DemoDlg.h" #include "ModifyDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CDemoDlg dialog CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CDemoDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDemoDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); if (!SUCCEEDED(m_pConnection.CreateInstance(__uuidof(Connection)))) { m_pConnection = NULL; TRACE(_T("Database CreateInstance failed")); } if (!SUCCEEDED(m_pRecordset.CreateInstance(__uuidof(Recordset)))) { m_pRecordset = NULL; TRACE(_T("Recordset CreateInstance Failed!")); } //打开数据库 CString strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= Demo.mdb"); if (!OpenDatabase(strConnect)) { AfxMessageBox(_T("数据库打开失败。")); return; } //打开记录集 CString strSource = _T("SELECT * FROM DemoTable"); if (!OpenRecordset(strSource)) { AfxMessageBox(_T("记录集打开失败。")); return; } } CDemoDlg::~CDemoDlg() { m_pRecordset->Close(); m_pConnection->Close(); } void CDemoDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDemoDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDemoDlg, CDialog) //{{AFX_MSG_MAP(CDemoDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_MODIFY, OnModify) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDemoDlg message handlers BOOL CDemoDlg::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 //初始化ListCtrl CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST); pListCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); pListCtrl->InsertColumn(1, _T("序号"), LVCFMT_CENTER, 80); pListCtrl->InsertColumn(2, _T("姓名"), LVCFMT_CENTER, 100); pListCtrl->InsertColumn(3, _T("年龄"), LVCFMT_CENTER, 100); //读取记录集 if (m_pRecordset->BOF && m_pRecordset->adoEOF) { return FALSE; } m_pRecordset->MoveFirst(); int n = 0; while (!m_pRecordset->adoEOF) { _variant_t varValue; CString strName = _T(""); int nAge = 0; varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME"))); if (varValue.vt != VT_NULL) { strName = varValue.bstrVal; } else { strName = _T(""); } varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE"))); if (varValue.vt != VT_NULL) { nAge = varValue.intVal; } else { nAge = 0; } //刷新ListCtrl CString strText = _T(""); strText.Format(_T("%d"), n + 1); pListCtrl->InsertItem(n, strText); strText.Format(_T("%s"), strName); pListCtrl->SetItemText(n, 1, strText); strText.Format(_T("%d"), nAge); pListCtrl->SetItemText(n, 2, strText); n++; m_pRecordset->MoveNext(); } return TRUE; } void CDemoDlg::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 CDemoDlg::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 CDemoDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } BOOL CDemoDlg::OpenDatabase(LPCTSTR lpszConnect, long nOptions) { ASSERT(m_pConnection != NULL); ASSERT(lpszConnect != NULL); ASSERT(AfxIsValidString(lpszConnect)); //打开数据库连接 try { return SUCCEEDED(m_pConnection->Open(_bstr_t(lpszConnect), _T(""), _T(""), nOptions)); } catch (_com_error& e) { TRACE(_T("%s\n"), e.ErrorMessage()); return FALSE; } } BOOL CDemoDlg::CloseDatabase() { ASSERT(m_pConnection != NULL); //关闭数据库连接 try { if (m_pConnection->State & adStateOpen) { return SUCCEEDED(m_pConnection->Close()); } else { return TRUE; } } catch (_com_error& e) { TRACE(_T("%s\n"), e.ErrorMessage()); return FALSE; } } BOOL CDemoDlg::OpenRecordset(LPCTSTR lpszSource, long nCursorType, long nLockType, long nOptions) { ASSERT(m_pConnection != NULL); ASSERT(m_pRecordset != NULL); ASSERT(lpszSource != NULL); ASSERT(AfxIsValidString(lpszSource)); //打开记录集 try { return (SUCCEEDED(m_pRecordset->Open(_variant_t(lpszSource), m_pConnection.GetInterfacePtr(), (CursorTypeEnum)nCursorType, (LockTypeEnum)nLockType, nOptions))); } catch(_com_error e) { TRACE(_T("%s\n"), e.ErrorMessage()); return FALSE; } } BOOL CDemoDlg::CloseRecorset() { ASSERT(m_pRecordset != NULL); //关闭记录集 try { if (m_pRecordset->State & adStateOpen) { return SUCCEEDED(m_pRecordset->Close()); } else { return TRUE; } } catch (_com_error e) { TRACE(_T("%s\n"), e.ErrorMessage()); return FALSE; } } void CDemoDlg::OnModify() { if (!(m_pRecordset->State & adStateOpen)) { AfxMessageBox((_T("记录集未打开。"))); return; } //获得记录集当前位置 CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST); POSITION pos = pListCtrl->GetFirstSelectedItemPosition(); if (pos == NULL) { return; } int nItem = pListCtrl->GetNextSelectedItem(pos); CModifyDlg dlg; dlg.m_strName = pListCtrl->GetItemText(nItem, 1); dlg.m_nAge = atoi(pListCtrl->GetItemText(nItem, 2)); if (dlg.DoModal() != IDOK) { return; } //修改记录集 if (m_pRecordset->BOF && m_pRecordset->adoEOF) { return; } m_pRecordset->MoveFirst(); m_pRecordset->Move((long)nItem); m_pRecordset->PutCollect(_variant_t(_T("NAME")), _variant_t(dlg.m_strName)); m_pRecordset->PutCollect(_variant_t(_T("AGE")), _variant_t((long)dlg.m_nAge)); m_pRecordset->Update(); //读取记录集 pListCtrl->DeleteAllItems(); if (m_pRecordset->BOF && m_pRecordset->adoEOF) { return; } m_pRecordset->MoveFirst(); int n = 0; while (!m_pRecordset->adoEOF) { _variant_t varValue; CString strName = _T(""); int nAge = 0; varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME"))); if (varValue.vt != VT_NULL) { strName = varValue.bstrVal; } else { strName = _T(""); } varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE"))); if (varValue.vt != VT_NULL) { nAge = varValue.intVal; } else { nAge = 0; } //刷新ListCtrl CString strText = _T(""); strText.Format(_T("%d"), n + 1); pListCtrl->InsertItem(n, strText); strText.Format(_T("%s"), strName); pListCtrl->SetItemText(n, 1, strText); strText.Format(_T("%d"), nAge); pListCtrl->SetItemText(n, 2, strText); n++; m_pRecordset->MoveNext(); } }