OLE_DATABASEVIEW.CPP
上传用户:sygkzy
上传日期:2013-03-13
资源大小:150k
文件大小:8k
源码类别:

Oracle数据库

开发平台:

Visual C++

  1. // ole_databaseView.cpp : implementation of the COle_databaseView class
  2. //
  3. #include "stdafx.h"
  4. #include "ole_database.h"
  5. #include "ole_databaseDoc.h"
  6. #include "ole_databaseView.h"
  7. #include "connect.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // COle_databaseView
  15. IMPLEMENT_DYNCREATE(COle_databaseView, CView)
  16. BEGIN_MESSAGE_MAP(COle_databaseView, CView)
  17. //{{AFX_MSG_MAP(COle_databaseView)
  18. ON_COMMAND(ID_CONNECT, OnConnect)
  19. ON_UPDATE_COMMAND_UI(ID_CONNECT, OnUpdateConnect)
  20. ON_COMMAND(ID_DISCONNECT, OnDisconnect)
  21. ON_UPDATE_COMMAND_UI(ID_DISCONNECT, OnUpdateDisconnect)
  22. ON_COMMAND(ID_EXECUTESQL, OnExecutesql)
  23. ON_UPDATE_COMMAND_UI(ID_EXECUTESQL, OnUpdateExecutesql)
  24. ON_WM_ERASEBKGND()
  25. ON_WM_SIZE()
  26. ON_WM_LBUTTONDOWN()
  27. ON_COMMAND(ID_SELECTTABLE, OnSelecttable)
  28. ON_UPDATE_COMMAND_UI(ID_SELECTTABLE, OnUpdateSelecttable)
  29. ON_COMMAND(ID_CLEARESQL, OnClearesql)
  30. ON_UPDATE_COMMAND_UI(ID_CLEARESQL, OnUpdateClearesql)
  31. ON_COMMAND(ID_CLEARRESULT, OnClearresult)
  32. ON_UPDATE_COMMAND_UI(ID_CLEARRESULT, OnUpdateClearresult)
  33. ON_COMMAND(ID_FILE_NEW, OnFileNew)
  34. ON_COMMAND(ID_FILE_SAVE, OnFileSave)
  35. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  36. //}}AFX_MSG_MAP
  37. // Standard printing commands
  38. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  39. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  40. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  41. END_MESSAGE_MAP()
  42. /////////////////////////////////////////////////////////////////////////////
  43. // COle_databaseView construction/destruction
  44. COle_databaseView::COle_databaseView()
  45. {
  46. OStartup();
  47. }
  48. COle_databaseView::~COle_databaseView()
  49. {
  50. OShutdown();
  51. }
  52. BOOL COle_databaseView::PreCreateWindow(CREATESTRUCT& cs)
  53. {
  54. // TODO: Modify the Window class or styles here by modifying
  55. //  the CREATESTRUCT cs
  56.     //cs.style&= ~FWS_ADDTOTITLE ;
  57. return CView::PreCreateWindow(cs);
  58. }
  59. /////////////////////////////////////////////////////////////////////////////
  60. // COle_databaseView drawing
  61. void COle_databaseView::OnDraw(CDC* pDC)
  62. {
  63. COle_databaseDoc* pDoc = GetDocument();
  64. ASSERT_VALID(pDoc);
  65. // TODO: add draw code for native data here
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. // COle_databaseView printing
  69. BOOL COle_databaseView::OnPreparePrinting(CPrintInfo* pInfo)
  70. {
  71. // default preparation
  72. return DoPreparePrinting(pInfo);
  73. }
  74. void COle_databaseView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  75. {
  76. // TODO: add extra initialization before printing
  77. }
  78. void COle_databaseView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  79. {
  80. // TODO: add cleanup after printing
  81. }
  82. /////////////////////////////////////////////////////////////////////////////
  83. // COle_databaseView diagnostics
  84. #ifdef _DEBUG
  85. void COle_databaseView::AssertValid() const
  86. {
  87. CView::AssertValid();
  88. }
  89. void COle_databaseView::Dump(CDumpContext& dc) const
  90. {
  91. CView::Dump(dc);
  92. }
  93. COle_databaseDoc* COle_databaseView::GetDocument() // non-debug version is inline
  94. {
  95. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COle_databaseDoc)));
  96. return (COle_databaseDoc*)m_pDocument;
  97. }
  98. #endif //_DEBUG
  99. /////////////////////////////////////////////////////////////////////////////
  100. // COle_databaseView message handlers
  101. BOOL COle_databaseView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  102. {
  103. CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  104. m_sqledit.Create(WS_CHILD|WS_VISIBLE|ES_MULTILINE|
  105. ES_AUTOVSCROLL|WS_EX_RIGHTSCROLLBAR|WS_BORDER,CRect(0,0,0,0),this,ID_SQLEDIT);
  106. m_flexgrid.Create("ABC",WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),
  107. this,ID_FLEXGRID,NULL,FALSE);
  108. m_flexgrid.SetAllowUserResizing(1);
  109. OnClearresult();
  110. return TRUE;
  111. }
  112. BOOL COle_databaseView::OnCommand(WPARAM wParam, LPARAM lParam) 
  113. {
  114. switch(LOWORD(wParam))
  115. {
  116. case ID_SQLDATA :
  117. //switch(HIWORD(wParam)
  118. // case WM_C
  119. break;
  120. default:
  121. return CView::OnCommand(wParam, lParam);
  122. }
  123. return CView::OnCommand(wParam, lParam);
  124. }
  125. BOOL COle_databaseView::DestroyWindow()
  126. {
  127.     if(odb.IsOpen())
  128. {
  129. odb.Close();
  130. }
  131. return CView::DestroyWindow();
  132. }
  133. void COle_databaseView::OnConnect() 
  134. {
  135. CConnect m_connect;
  136. CString m_dbname;
  137. CString m_username;
  138. CString m_userpsw;
  139. if(m_connect.DoModal()!=IDOK)
  140. return;
  141. m_connect.GetData(m_dbname,m_username,m_userpsw);
  142. odb.Open(m_dbname,m_username,m_userpsw);
  143. if(!odb.IsOpen())
  144. {
  145. const char *m_errortext=odb.GetErrorText();
  146. this->MessageBox(m_errortext,"错误",MB_OK|MB_ICONERROR);
  147. return ;
  148. }
  149. }
  150. void COle_databaseView::OnUpdateConnect(CCmdUI* pCmdUI) 
  151. {
  152. pCmdUI->Enable(!odb.IsOpen());
  153. }
  154. void COle_databaseView::OnDisconnect() 
  155. {
  156. if (!odb.IsOpen())
  157. {
  158. ::MessageBox(this->m_hWnd," the database is closed!","error",MB_OK);
  159. }else
  160. {
  161. odb.Close();
  162. }
  163. }
  164. void COle_databaseView::OnUpdateDisconnect(CCmdUI* pCmdUI) 
  165. {
  166. pCmdUI->Enable(odb.IsOpen());
  167. }
  168. void COle_databaseView::OnExecutesql() 
  169. {
  170. ///DeleteFromListBox();
  171. CString m_sSql,m_tmp,m_tmp2;
  172. int m_fieldcount,m_recordcount,m_i;
  173. m_sqledit.GetWindowText(m_sSql);
  174. odyn.Open(odb,m_sSql);
  175. m_fieldcount=odyn.GetFieldCount();
  176. //加入列名
  177. m_flexgrid.SetCols(m_fieldcount+1);
  178. m_flexgrid.SetTextMatrix(0,0,"序号");
  179. for(int i=0;i<m_fieldcount;i++)
  180. {
  181. m_flexgrid.SetTextMatrix(0,i+1,odyn.GetFieldOriginalName(i));
  182. }
  183. //加入数据
  184. i=1;
  185. m_recordcount=odyn.GetRecordCount();
  186. m_flexgrid.SetRows(m_recordcount+1);
  187. while(!odyn.IsEOF())
  188. {
  189. char x[10];
  190. ::itoa(i,x,10);
  191. m_flexgrid.SetTextMatrix(i,0,x);
  192. for(m_i=0;m_i<m_fieldcount;m_i++)
  193. {
  194. odyn.GetFieldValue(m_i,&ovalue);
  195. m_flexgrid.SetTextMatrix(i,m_i+1,&(*ovalue));
  196. }
  197. odyn.MoveNext(); 
  198. i++;
  199. }
  200. }
  201. void COle_databaseView::OnUpdateExecutesql(CCmdUI* pCmdUI) 
  202. {
  203. CString m_tmp;
  204. m_sqledit.GetWindowText(m_tmp);
  205. pCmdUI->Enable(!m_tmp.IsEmpty());
  206. }
  207. BOOL COle_databaseView::OnEraseBkgnd(CDC* pDC) 
  208. {
  209.     // Set brush to desired background color 
  210.     CBrush backBrush(RGB(148, 148, 148)); 
  211.     
  212.     // Save old brush 
  213.     CBrush* pOldBrush = pDC->SelectObject(&backBrush); 
  214.     CRect rect; 
  215.     pDC->GetClipBox(&rect); 
  216.     
  217.     // Erase the area needed 
  218.     pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); 
  219.     pDC->SelectObject(pOldBrush); 
  220. //CView::OnEraseBkgnd(pDC);
  221. return  TRUE;
  222. }
  223. void COle_databaseView::OnSize(UINT nType, int cx, int cy) 
  224. {
  225. CView::OnSize(nType, cx, cy);
  226. ::SetWindowPos(m_sqledit.m_hWnd,HWND_TOP,0,0,cx,cy/3*1,SWP_SHOWWINDOW);
  227. ::SetWindowPos(m_flexgrid.m_hWnd,HWND_TOP,0,cy/3*1,cx,cy/3*2,SWP_SHOWWINDOW);
  228. }
  229. void COle_databaseView::OnLButtonDown(UINT nFlags, CPoint point) 
  230. {
  231. CView::OnLButtonDown(nFlags, point);
  232. }
  233. void COle_databaseView::OnSelecttable() 
  234. {
  235. CSelectTable m_selecttable;
  236. m_selecttable.odb=this->odb;
  237. if(m_selecttable.DoModal()!=IDOK)
  238. return;
  239. m_sqledit.SetWindowText(m_selecttable.m_sSql);
  240. }
  241. void COle_databaseView::OnUpdateSelecttable(CCmdUI* pCmdUI) 
  242. {
  243. pCmdUI->Enable(odb.IsOpen());
  244. }
  245. void COle_databaseView::OnClearesql() 
  246. {
  247. m_sqledit.SetWindowText("");
  248. }
  249. void COle_databaseView::OnUpdateClearesql(CCmdUI* pCmdUI) 
  250. {
  251. CString m_tmp;
  252. m_sqledit.GetWindowText(m_tmp);
  253. pCmdUI->Enable(!m_tmp.IsEmpty());
  254. }
  255. void COle_databaseView::OnClearresult() 
  256. {
  257. m_flexgrid.Clear();
  258. m_flexgrid.SetCols(1);
  259. m_flexgrid.SetRows(1);
  260. }
  261. void COle_databaseView::OnUpdateClearresult(CCmdUI* pCmdUI) 
  262. {
  263. }
  264. void COle_databaseView::OnFileNew() 
  265. {
  266. if(m_sqledit.GetModify())
  267. {
  268. if(this->MessageBox("数据已经被更改,是否要保存?","提示",MB_YESNO)==IDYES)
  269. {
  270. OnFileSave();
  271. m_sqledit.SetModify(FALSE);
  272. }
  273. OnClearesql();
  274. OnClearresult();
  275. }
  276. }
  277. void COle_databaseView::OnFileSave() 
  278. {
  279. // TODO: Add your command handler code here
  280. }
  281. void COle_databaseView::OnFileOpen() 
  282. {
  283. if(m_sqledit.GetModify())
  284. {
  285. if(this->MessageBox("数据已经被更改,是否要保存?","提示",MB_YESNO)==IDYES)
  286. {
  287. OnFileSave();
  288. m_sqledit.SetModify(FALSE);
  289. }
  290. }
  291. }