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

书籍源码

开发平台:

Visual C++

  1. // SearchByMultiStrDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SearchByMultiStr.h"
  5. #include "SearchByMultiStrDlg.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. // CSearchByMultiStrDlg dialog
  51. CSearchByMultiStrDlg::CSearchByMultiStrDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CSearchByMultiStrDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CSearchByMultiStrDlg)
  55. m_bAnd = FALSE;
  56. m_strComment = _T("");
  57. m_bComplete = FALSE;
  58. m_strName = _T("");
  59. //}}AFX_DATA_INIT
  60. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  61. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  62. }
  63. void CSearchByMultiStrDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CDialog::DoDataExchange(pDX);
  66. //{{AFX_DATA_MAP(CSearchByMultiStrDlg)
  67. DDX_Control(pDX, IDC_STUDENTS, m_StudentsLst);
  68. DDX_Check(pDX, IDC_AND, m_bAnd);
  69. DDX_Text(pDX, IDC_COMMENT, m_strComment);
  70. DDX_Check(pDX, IDC_COMPLETE, m_bComplete);
  71. DDX_Text(pDX, IDC_NAME, m_strName);
  72. //}}AFX_DATA_MAP
  73. }
  74. BEGIN_MESSAGE_MAP(CSearchByMultiStrDlg, CDialog)
  75. //{{AFX_MSG_MAP(CSearchByMultiStrDlg)
  76. ON_WM_SYSCOMMAND()
  77. ON_WM_PAINT()
  78. ON_WM_QUERYDRAGICON()
  79. ON_WM_DESTROY()
  80. ON_BN_CLICKED(IDC_SEARCH, OnSearch)
  81. //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CSearchByMultiStrDlg message handlers
  85. BOOL CSearchByMultiStrDlg::OnInitDialog()
  86. {
  87. CDialog::OnInitDialog();
  88. // Add "About..." menu item to system menu.
  89. // IDM_ABOUTBOX must be in the system command range.
  90. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  91. ASSERT(IDM_ABOUTBOX < 0xF000);
  92. CMenu* pSysMenu = GetSystemMenu(FALSE);
  93. if (pSysMenu != NULL)
  94. {
  95. CString strAboutMenu;
  96. strAboutMenu.LoadString(IDS_ABOUTBOX);
  97. if (!strAboutMenu.IsEmpty())
  98. {
  99. pSysMenu->AppendMenu(MF_SEPARATOR);
  100. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  101. }
  102. }
  103. // Set the icon for this dialog.  The framework does this automatically
  104. //  when the application's main window is not a dialog
  105. SetIcon(m_hIcon, TRUE); // Set big icon
  106. SetIcon(m_hIcon, FALSE); // Set small icon
  107. // TODO: Add extra initialization here
  108. //初始化环境
  109. ::CoInitialize(NULL);
  110. //创建并打开数据库连接对象
  111. _variant_t vFieldValue;
  112. CString strFieldValue;
  113. m_pCon.CreateInstance(__uuidof(Connection));
  114. m_pCon->Open("students","","",NULL);
  115. //创建并打开记录集对象
  116. m_pRs.CreateInstance(__uuidof(Recordset));
  117. m_pRs->Open("select* from table1",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  118. return TRUE;  // return TRUE  unless you set the focus to a control
  119. }
  120. void CSearchByMultiStrDlg::OnSysCommand(UINT nID, LPARAM lParam)
  121. {
  122. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  123. {
  124. CAboutDlg dlgAbout;
  125. dlgAbout.DoModal();
  126. }
  127. else
  128. {
  129. CDialog::OnSysCommand(nID, lParam);
  130. }
  131. }
  132. // If you add a minimize button to your dialog, you will need the code below
  133. //  to draw the icon.  For MFC applications using the document/view model,
  134. //  this is automatically done for you by the framework.
  135. void CSearchByMultiStrDlg::OnPaint() 
  136. {
  137. if (IsIconic())
  138. {
  139. CPaintDC dc(this); // device context for painting
  140. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  141. // Center icon in client rectangle
  142. int cxIcon = GetSystemMetrics(SM_CXICON);
  143. int cyIcon = GetSystemMetrics(SM_CYICON);
  144. CRect rect;
  145. GetClientRect(&rect);
  146. int x = (rect.Width() - cxIcon + 1) / 2;
  147. int y = (rect.Height() - cyIcon + 1) / 2;
  148. // Draw the icon
  149. dc.DrawIcon(x, y, m_hIcon);
  150. }
  151. else
  152. {
  153. CDialog::OnPaint();
  154. }
  155. }
  156. // The system calls this to obtain the cursor to display while the user drags
  157. //  the minimized window.
  158. HCURSOR CSearchByMultiStrDlg::OnQueryDragIcon()
  159. {
  160. return (HCURSOR) m_hIcon;
  161. }
  162. void CSearchByMultiStrDlg::OnDestroy() 
  163. {
  164. //关闭记录和连接
  165. m_pRs->Close();
  166. m_pCon->Close();
  167. //释放环境
  168. ::CoUninitialize();
  169. CDialog::OnDestroy();
  170. }
  171. void CSearchByMultiStrDlg::OnSearch() 
  172. {
  173. //情况列表框的数据
  174. while(m_StudentsLst.GetCount()>0)
  175. {
  176. m_StudentsLst.DeleteString(0);
  177. }
  178. UpdateData(TRUE);
  179. //关闭上一次查询的记录集
  180. m_pRs->Close();
  181. //构造SQL检索语句
  182. CString strSql;
  183. if(m_bComplete)
  184. {//完全匹配
  185. if(m_bAnd)
  186. {//与条件
  187. strSql.Format("select * from table1 where name = '%s' and comment = '%s'",m_strName,m_strComment);
  188. }
  189. else
  190. {
  191. strSql.Format("select * from table1 where name = '%s' or comment = '%s'",m_strName,m_strComment);
  192. }
  193. }
  194. else
  195. {//模糊查询
  196. if(m_bAnd)
  197. {//与条件
  198. strSql = "select * from table1 where name like '%" + m_strName + "%'"
  199. + "and comment like '%" + m_strComment + "%'";
  200. }
  201. else
  202. {
  203. strSql = "select * from table1 where name like '%" + m_strName + "%'"
  204. + "or comment like '%" + m_strComment + "%'";
  205. }
  206. }
  207. //检索
  208. BSTR bsSql = strSql.AllocSysString();
  209. m_pRs->Open((CComVariant)bsSql,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  210. //将查询结果在列表框中显示出来
  211. while(VARIANT_FALSE == m_pRs->EndOfFile)
  212. {
  213. _variant_t vFieldValue;
  214. CString strID,strName,strComment;
  215. CString strItem;
  216. vFieldValue = m_pRs->GetCollect("id");
  217. strID = (char*)_bstr_t(vFieldValue);
  218. vFieldValue.Clear();
  219. vFieldValue = m_pRs->GetCollect("name");
  220. strName = (char*)_bstr_t(vFieldValue);
  221. vFieldValue.Clear();
  222. vFieldValue = m_pRs->GetCollect("comment");
  223. strComment = (char*)_bstr_t(vFieldValue);
  224. vFieldValue.Clear();
  225. strItem.Format("学号为%s的%s同学是%s",strID,strName,strComment);
  226. m_StudentsLst.AddString(strItem);
  227. m_pRs->MoveNext();
  228. }
  229. }