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

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. ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  82. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  83. //}}AFX_MSG_MAP
  84. END_MESSAGE_MAP()
  85. /////////////////////////////////////////////////////////////////////////////
  86. // COffDemoDlg message handlers
  87. BOOL COffDemoDlg::OnInitDialog()
  88. {
  89. CDialog::OnInitDialog();
  90. // Add "About..." menu item to system menu.
  91. // IDM_ABOUTBOX must be in the system command range.
  92. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  93. ASSERT(IDM_ABOUTBOX < 0xF000);
  94. CMenu* pSysMenu = GetSystemMenu(FALSE);
  95. if (pSysMenu != NULL)
  96. {
  97. CString strAboutMenu;
  98. strAboutMenu.LoadString(IDS_ABOUTBOX);
  99. if (!strAboutMenu.IsEmpty())
  100. {
  101. pSysMenu->AppendMenu(MF_SEPARATOR);
  102. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  103. }
  104. }
  105. // Set the icon for this dialog.  The framework does this automatically
  106. //  when the application's main window is not a dialog
  107. SetIcon(m_hIcon, TRUE); // Set big icon
  108. SetIcon(m_hIcon, FALSE); // Set small icon
  109. ::CoInitialize(NULL);
  110. // TODO: Add extra initialization here
  111. return TRUE;  // return TRUE  unless you set the focus to a control
  112. }
  113. void COffDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
  114. {
  115. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  116. {
  117. CAboutDlg dlgAbout;
  118. dlgAbout.DoModal();
  119. }
  120. else
  121. {
  122. CDialog::OnSysCommand(nID, lParam);
  123. }
  124. }
  125. // If you add a minimize button to your dialog, you will need the code below
  126. //  to draw the icon.  For MFC applications using the document/view model,
  127. //  this is automatically done for you by the framework.
  128. void COffDemoDlg::OnPaint() 
  129. {
  130. if (IsIconic())
  131. {
  132. CPaintDC dc(this); // device context for painting
  133. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  134. // Center icon in client rectangle
  135. int cxIcon = GetSystemMetrics(SM_CXICON);
  136. int cyIcon = GetSystemMetrics(SM_CYICON);
  137. CRect rect;
  138. GetClientRect(&rect);
  139. int x = (rect.Width() - cxIcon + 1) / 2;
  140. int y = (rect.Height() - cyIcon + 1) / 2;
  141. // Draw the icon
  142. dc.DrawIcon(x, y, m_hIcon);
  143. }
  144. else
  145. {
  146. CDialog::OnPaint();
  147. }
  148. }
  149. // The system calls this to obtain the cursor to display while the user drags
  150. //  the minimized window.
  151. HCURSOR COffDemoDlg::OnQueryDragIcon()
  152. {
  153. return (HCURSOR) m_hIcon;
  154. }
  155. void COffDemoDlg::OnButton1() 
  156. {
  157. // TODO: Add your control notification handler code here
  158. _ConnectionPtr conn;
  159. conn.CreateInstance(__uuidof(Connection));
  160. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena"),"",_bstr_t("tiger"),-1);
  161. rs.CreateInstance(__uuidof(Recordset));
  162. _bstr_t sqlobj("select * from ocybooks");
  163. rs->Open((_variant_t)sqlobj,
  164. conn.GetInterfacePtr(),
  165. adOpenStatic,
  166. adLockBatchOptimistic,
  167. -1);
  168.     //显示第一条记录;
  169. this->show();
  170. }
  171. //显示数据,默认为第一条
  172. void COffDemoDlg::show()
  173. {
  174. if(!rs->adEOF && !rs->BOF)
  175. {
  176. this->m_isbn=(UINT)(long)(rs->GetCollect("BOOKISNB"));
  177. this->m_name=(CString)(LPCTSTR)(_bstr_t)(rs->GetCollect("BOOKNAME"));
  178. this->UpdateData(FALSE);
  179. }
  180. }
  181. //查看后一条数据
  182. void COffDemoDlg::OnButton7() 
  183. {
  184. // TODO: Add your control notification handler code here
  185. if(!rs->adEOF)
  186. {
  187. rs->MoveNext();
  188. show();
  189. }
  190. else
  191. {
  192. MessageBox("End!!!");
  193. }
  194. }
  195. //查看前一条数据
  196. void COffDemoDlg::OnButton6() 
  197. {
  198. // TODO: Add your control notification handler code here
  199. if(!rs->BOF)
  200. {
  201. rs->MovePrevious();
  202. show();
  203. }
  204. else
  205. {
  206. MessageBox("Begin!!!");
  207. }
  208. }
  209. //显示第一条数据
  210. void COffDemoDlg::OnButton9() 
  211. {
  212. // TODO: Add your control notification handler code here
  213. if(!rs->BOF)
  214. {
  215. rs->MoveFirst();
  216. show();
  217. }
  218. else
  219. {
  220. MessageBox("Begin!!!");
  221. }
  222. }
  223. //新增记录时先清空文本框
  224. void COffDemoDlg::OnButton3() 
  225. {
  226. // TODO: Add your control notification handler code here
  227. this->m_isbn=0;
  228. this->m_name="";
  229. this->m_author="";
  230. this->UpdateData(FALSE);
  231. }
  232. //更新数据库数据
  233. void COffDemoDlg::OnButton2() 
  234. {
  235. // TODO: Add your control notification handler code here
  236. this->UpdateData(TRUE);
  237. this->rs->AddNew();
  238. this->rs->GetFields()->GetItem("BookIsnb")->Value=_variant_t((long)(this->m_isbn));
  239. this->rs->GetFields()->GetItem("BookName")->Value=_variant_t(this->m_name.AllocSysString());
  240. this->rs->GetFields()->GetItem("BookIsnb")->Value=_variant_t(this->m_author.AllocSysString());
  241. this->rs->Update();
  242. this->rs->UpdateBatch(adAffectAll);
  243. }
  244. //删除数据库数据
  245. void COffDemoDlg::OnButton4() 
  246. {
  247. // TODO: Add your control notification handler code here
  248. this->rs->Delete(adAffectCurrent);
  249. this->rs->Update();
  250. this->rs->UpdateBatch(adAffectAll);
  251. }
  252. //更改数据
  253. void COffDemoDlg::OnButton5() 
  254. {
  255. // TODO: Add your control notification handler code here
  256. this->UpdateData(TRUE);
  257. this->rs->GetFields()->GetItem("BookIsnb")->Value=_variant_t((long)(this->m_isbn));
  258. this->rs->GetFields()->GetItem("BookName")->Value=_variant_t(this->m_name.AllocSysString());
  259. this->rs->GetFields()->GetItem("BookAuthor")->Value=_variant_t(this->m_author.AllocSysString());
  260. this->rs->Update();
  261. this->rs->UpdateBatch(adAffectAll);
  262. }