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

书籍源码

开发平台:

Visual C++

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