SearchByTmDlg.cpp
上传用户:lqt88888
上传日期:2009-12-14
资源大小:905k
文件大小:6k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // SearchByTmDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SearchByTm.h"
  5. #include "SearchByTmDlg.h"
  6. #include "atlbase.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CSearchByTmDlg dialog
  51. CSearchByTmDlg::CSearchByTmDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CSearchByTmDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CSearchByTmDlg)
  55. m_tmEnd = 0;
  56. m_tmStart = 0;
  57. //}}AFX_DATA_INIT
  58. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  59. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  60. }
  61. void CSearchByTmDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CSearchByTmDlg)
  65. DDX_Control(pDX, IDC_STUDENTS, m_StudentsLst);
  66. DDX_DateTimeCtrl(pDX, IDC_ENDTM, m_tmEnd);
  67. DDX_DateTimeCtrl(pDX, IDC_STARTTM, m_tmStart);
  68. //}}AFX_DATA_MAP
  69. }
  70. BEGIN_MESSAGE_MAP(CSearchByTmDlg, CDialog)
  71. //{{AFX_MSG_MAP(CSearchByTmDlg)
  72. ON_WM_SYSCOMMAND()
  73. ON_WM_PAINT()
  74. ON_WM_QUERYDRAGICON()
  75. ON_BN_CLICKED(ID_SEARCH, OnSearch)
  76. ON_WM_DESTROY()
  77. //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CSearchByTmDlg message handlers
  81. BOOL CSearchByTmDlg::OnInitDialog()
  82. {
  83. CDialog::OnInitDialog();
  84. // Add "About..." menu item to system menu.
  85. // IDM_ABOUTBOX must be in the system command range.
  86. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  87. ASSERT(IDM_ABOUTBOX < 0xF000);
  88. CMenu* pSysMenu = GetSystemMenu(FALSE);
  89. if (pSysMenu != NULL)
  90. {
  91. CString strAboutMenu;
  92. strAboutMenu.LoadString(IDS_ABOUTBOX);
  93. if (!strAboutMenu.IsEmpty())
  94. {
  95. pSysMenu->AppendMenu(MF_SEPARATOR);
  96. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  97. }
  98. }
  99. // Set the icon for this dialog.  The framework does this automatically
  100. //  when the application's main window is not a dialog
  101. SetIcon(m_hIcon, TRUE); // Set big icon
  102. SetIcon(m_hIcon, FALSE); // Set small icon
  103. // TODO: Add extra initialization here
  104. //初始化环境
  105. ::CoInitialize(NULL);
  106. //创建并打开数据库连接对象
  107. _variant_t vFieldValue;
  108. CString strFieldValue;
  109. m_pCon.CreateInstance(__uuidof(Connection));
  110. m_pCon->Open("students","","",NULL);
  111. //创建并打开记录集对象
  112. m_pRs.CreateInstance(__uuidof(Recordset));
  113. m_pRs->Open("select* from table1",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  114. return TRUE;  // return TRUE  unless you set the focus to a control
  115. }
  116. void CSearchByTmDlg::OnSysCommand(UINT nID, LPARAM lParam)
  117. {
  118. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  119. {
  120. CAboutDlg dlgAbout;
  121. dlgAbout.DoModal();
  122. }
  123. else
  124. {
  125. CDialog::OnSysCommand(nID, lParam);
  126. }
  127. }
  128. // If you add a minimize button to your dialog, you will need the code below
  129. //  to draw the icon.  For MFC applications using the document/view model,
  130. //  this is automatically done for you by the framework.
  131. void CSearchByTmDlg::OnPaint() 
  132. {
  133. if (IsIconic())
  134. {
  135. CPaintDC dc(this); // device context for painting
  136. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  137. // Center icon in client rectangle
  138. int cxIcon = GetSystemMetrics(SM_CXICON);
  139. int cyIcon = GetSystemMetrics(SM_CYICON);
  140. CRect rect;
  141. GetClientRect(&rect);
  142. int x = (rect.Width() - cxIcon + 1) / 2;
  143. int y = (rect.Height() - cyIcon + 1) / 2;
  144. // Draw the icon
  145. dc.DrawIcon(x, y, m_hIcon);
  146. }
  147. else
  148. {
  149. CDialog::OnPaint();
  150. }
  151. }
  152. // The system calls this to obtain the cursor to display while the user drags
  153. //  the minimized window.
  154. HCURSOR CSearchByTmDlg::OnQueryDragIcon()
  155. {
  156. return (HCURSOR) m_hIcon;
  157. }
  158. void CSearchByTmDlg::OnSearch() 
  159. {
  160. //情况列表框的数据
  161. while(m_StudentsLst.GetCount()>0)
  162. {
  163. m_StudentsLst.DeleteString(0);
  164. }
  165. UpdateData(TRUE);
  166. //关闭上一次查询的记录集
  167. m_pRs->Close();
  168. //格式化时间
  169. CString strBegin,strEnd;
  170. strBegin = m_tmStart.Format("%Y-%m-%d %H:%M:%S");
  171. strEnd = m_tmEnd.Format("%Y-%m-%d %H:%M:%S");
  172. //构造SQL检索语句
  173. CString strSql;
  174. strSql.Format("select * from table1 where entertm >= CDate('%s') and entertm <= CDate('%s')",strBegin,strEnd);
  175. //检索
  176. BSTR bsSql = strSql.AllocSysString();
  177. m_pRs->Open((CComVariant)bsSql,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  178. //将查询结果在列表框中显示出来
  179. while(VARIANT_FALSE == m_pRs->EndOfFile)
  180. {
  181. _variant_t vFieldValue;
  182. CString strID,strName,strEnterTm;
  183. CString strItem;
  184. vFieldValue = m_pRs->GetCollect("id");
  185. strID = (char*)_bstr_t(vFieldValue);
  186. vFieldValue.Clear();
  187. vFieldValue = m_pRs->GetCollect("name");
  188. strName = (char*)_bstr_t(vFieldValue);
  189. vFieldValue.Clear();
  190. vFieldValue = m_pRs->GetCollect("entertm");
  191. strEnterTm = (char*)_bstr_t(vFieldValue);
  192. vFieldValue.Clear();
  193. strItem.Format("学号为%s的%s同学的入学时间是%s",strID,strName,strEnterTm);
  194. m_StudentsLst.AddString(strItem);
  195. m_pRs->MoveNext();
  196. }
  197. }
  198. void CSearchByTmDlg::OnDestroy() 
  199. {
  200. //关闭记录和连接
  201. m_pRs->Close();
  202. m_pCon->Close();
  203. //释放环境
  204. ::CoUninitialize();
  205. CDialog::OnDestroy();
  206. }