OffDemoDlg.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:6k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // OffDemoDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "OffDemo.h"
  5. #include "OffDemoDlg.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. // COffDemoDlg dialog
  50. COffDemoDlg::COffDemoDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(COffDemoDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(COffDemoDlg)
  54. m_isbn = 0;
  55. m_name = _T("");
  56. m_author = _T("");
  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 COffDemoDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(COffDemoDlg)
  65. DDX_Text(pDX, IDC_EDIT1, m_isbn);
  66. DDX_Text(pDX, IDC_EDIT2, m_name);
  67. DDX_Text(pDX, IDC_EDIT3, m_author);
  68. //}}AFX_DATA_MAP
  69. }
  70. BEGIN_MESSAGE_MAP(COffDemoDlg, CDialog)
  71. //{{AFX_MSG_MAP(COffDemoDlg)
  72. ON_WM_SYSCOMMAND()
  73. ON_WM_PAINT()
  74. ON_WM_QUERYDRAGICON()
  75. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  76. ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
  77. ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
  78. ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
  79. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  80. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  81. //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83. /////////////////////////////////////////////////////////////////////////////
  84. // COffDemoDlg message handlers
  85. BOOL COffDemoDlg::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. ::CoInitialize(NULL);
  108. // TODO: Add extra initialization here
  109. return TRUE;  // return TRUE  unless you set the focus to a control
  110. }
  111. void COffDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
  112. {
  113. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  114. {
  115. CAboutDlg dlgAbout;
  116. dlgAbout.DoModal();
  117. }
  118. else
  119. {
  120. CDialog::OnSysCommand(nID, lParam);
  121. }
  122. }
  123. // If you add a minimize button to your dialog, you will need the code below
  124. //  to draw the icon.  For MFC applications using the document/view model,
  125. //  this is automatically done for you by the framework.
  126. void COffDemoDlg::OnPaint() 
  127. {
  128. if (IsIconic())
  129. {
  130. CPaintDC dc(this); // device context for painting
  131. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  132. // Center icon in client rectangle
  133. int cxIcon = GetSystemMetrics(SM_CXICON);
  134. int cyIcon = GetSystemMetrics(SM_CYICON);
  135. CRect rect;
  136. GetClientRect(&rect);
  137. int x = (rect.Width() - cxIcon + 1) / 2;
  138. int y = (rect.Height() - cyIcon + 1) / 2;
  139. // Draw the icon
  140. dc.DrawIcon(x, y, m_hIcon);
  141. }
  142. else
  143. {
  144. CDialog::OnPaint();
  145. }
  146. }
  147. // The system calls this to obtain the cursor to display while the user drags
  148. //  the minimized window.
  149. HCURSOR COffDemoDlg::OnQueryDragIcon()
  150. {
  151. return (HCURSOR) m_hIcon;
  152. }
  153. void COffDemoDlg::OnButton1() 
  154. {
  155. // TODO: Add your control notification handler code here
  156. _ConnectionPtr conn;
  157. conn.CreateInstance(__uuidof(Connection));
  158. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=csd0910;Data Source=lisongtao"),"",_bstr_t("csd0910"),-1);
  159. rs.CreateInstance(__uuidof(Recordset));
  160. _bstr_t sqlobj("select * from mybooks");
  161. rs->Open((_variant_t)sqlobj,
  162. conn.GetInterfacePtr(),
  163. adOpenStatic,
  164. adLockBatchOptimistic,//与UpdateBatch关联
  165. -1);
  166.     //显示第一条记录;
  167. this->show();
  168. }
  169. //显示数据,默认为第一条
  170. void COffDemoDlg::show()
  171. {
  172. if(!rs->adEOF && !rs->BOF)
  173. {
  174. this->m_isbn=(UINT)(long)(rs->GetCollect("BOOKISNB"));
  175. this->m_name=(CString)(LPCTSTR)(_bstr_t)(rs->GetCollect("BOOKNAME"));
  176. this->UpdateData(FALSE);
  177. }
  178. }
  179. //查看后一条数据
  180. void COffDemoDlg::OnButton7() 
  181. {
  182. // TODO: Add your control notification handler code here
  183. if(!rs->adEOF)
  184. {
  185. rs->MoveNext();
  186. show();
  187. }
  188. else
  189. {
  190. MessageBox("End!!!");
  191. }
  192. }
  193. //查看前一条数据
  194. void COffDemoDlg::OnButton6() 
  195. {
  196. // TODO: Add your control notification handler code here
  197. if(!rs->BOF)
  198. {
  199. rs->MovePrevious();
  200. show();
  201. }
  202. else
  203. {
  204. MessageBox("Begin!!!");
  205. }
  206. }
  207. //显示第一条数据
  208. void COffDemoDlg::OnButton9() 
  209. {
  210. // TODO: Add your control notification handler code here
  211. if(!rs->BOF)
  212. {
  213. rs->MoveFirst();
  214. show();
  215. }
  216. else
  217. {
  218. MessageBox("Begin!!!");
  219. }
  220. }
  221. //新增记录时先清空文本框
  222. void COffDemoDlg::OnButton3() 
  223. {
  224. // TODO: Add your control notification handler code here
  225. this->m_isbn=0;
  226. this->m_name="";
  227. this->m_author="";
  228. this->UpdateData(FALSE);//(更新后)把数据送到文本框
  229. }
  230. //更新数据库数据
  231. void COffDemoDlg::OnButton2() 
  232. {
  233. // TODO: Add your control notification handler code here
  234. this->UpdateData(TRUE);
  235. this->rs->AddNew();
  236. this->rs->GetFields()->GetItem("BookIsnb")->Value=_variant_t((long)(this->m_isbn));
  237. this->rs->GetFields()->GetItem("BookName")->Value=_variant_t(this->m_name.AllocSysString());
  238. this->rs->GetFields()->GetItem("BookIsnb")->Value=_variant_t(this->m_author.AllocSysString());
  239. this->rs->Update();
  240. this->rs->UpdateBatch(adAffectAll);
  241. }