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

书籍源码

开发平台:

Visual C++

  1. // SearchByStrDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SearchByStr.h"
  5. #include "SearchByStrDlg.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. // CSearchByStrDlg dialog
  51. CSearchByStrDlg::CSearchByStrDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CSearchByStrDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CSearchByStrDlg)
  55. m_strName = _T("");
  56. m_bComplete = FALSE;
  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 CSearchByStrDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CSearchByStrDlg)
  65. DDX_Control(pDX, IDC_STUDENTS_LST, m_StudentsLst);
  66. DDX_Text(pDX, IDC_NAME, m_strName);
  67. DDX_Check(pDX, IDC_COMPLETE, m_bComplete);
  68. //}}AFX_DATA_MAP
  69. }
  70. BEGIN_MESSAGE_MAP(CSearchByStrDlg, CDialog)
  71. //{{AFX_MSG_MAP(CSearchByStrDlg)
  72. ON_WM_SYSCOMMAND()
  73. ON_WM_PAINT()
  74. ON_WM_QUERYDRAGICON()
  75. ON_BN_CLICKED(IDC_SEARCH, OnSearch)
  76. ON_WM_DESTROY()
  77. //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CSearchByStrDlg message handlers
  81. BOOL CSearchByStrDlg::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 CSearchByStrDlg::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 CSearchByStrDlg::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 CSearchByStrDlg::OnQueryDragIcon()
  155. {
  156. return (HCURSOR) m_hIcon;
  157. }
  158. void CSearchByStrDlg::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. //构造SQL检索语句
  169. CString strSql;
  170. if(m_bComplete)
  171. {
  172. //完全匹配
  173. strSql.Format("select * from table1 where name = '%s'",m_strName);
  174. }
  175. else
  176. {
  177. //模糊查询
  178. strSql.Format("%s",m_strName);
  179. strSql = "select * from table1 where name like '%" + strSql + "%'";
  180. }
  181. //检索
  182. BSTR bsSql = strSql.AllocSysString();
  183. m_pRs->Open((CComVariant)bsSql,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  184. //将查询结果在列表框中显示出来
  185. while(VARIANT_FALSE == m_pRs->EndOfFile)
  186. {
  187. _variant_t vFieldValue;
  188. CString strID,strName,strEnterTm;
  189. CString strItem;
  190. vFieldValue = m_pRs->GetCollect("id");
  191. strID = (char*)_bstr_t(vFieldValue);
  192. vFieldValue.Clear();
  193. vFieldValue = m_pRs->GetCollect("name");
  194. strName = (char*)_bstr_t(vFieldValue);
  195. vFieldValue.Clear();
  196. vFieldValue = m_pRs->GetCollect("entertm");
  197. strEnterTm = (char*)_bstr_t(vFieldValue);
  198. vFieldValue.Clear();
  199. strItem.Format("学号为%s的%s同学的入学时间是%s",strID,strName,strEnterTm);
  200. m_StudentsLst.AddString(strItem);
  201. m_pRs->MoveNext();
  202. }
  203. }
  204. void CSearchByStrDlg::OnDestroy() 
  205. {
  206. //关闭记录和连接
  207. m_pRs->Close();
  208. m_pCon->Close();
  209. //释放环境
  210. ::CoUninitialize();
  211. CDialog::OnDestroy();
  212. }