home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c09 / odbc2 / odbc2view.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  5.7 KB  |  262 lines

  1. // odbc2View.cpp : implementation of the COdbc2View class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "odbc2.h"
  6.  
  7. #include "odbc2Doc.h"
  8. #include "MyRecordset2.h"
  9. #include "odbc2View.h"
  10.  
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // COdbc2View
  20.  
  21. IMPLEMENT_DYNCREATE(COdbc2View, CScrollView)
  22.  
  23. BEGIN_MESSAGE_MAP(COdbc2View, CScrollView)
  24.     //{{AFX_MSG_MAP(COdbc2View)
  25.     ON_COMMAND(ID_ODBC_ADD, OnOdbcAdd)
  26.     ON_COMMAND(ID_ODBC_OPEN, OnOdbcOpen)
  27.     ON_COMMAND(ID_ODBC_GENERIC, OnOdbcGeneric)
  28.     ON_COMMAND(ID_ODBC_ROWSET, OnOdbcRowset)
  29.     ON_COMMAND(ID_ODBC_QUERY, OnOdbcQuery)
  30.     ON_COMMAND(ID_ODBC_INFO, OnOdbcInfo)
  31.     ON_COMMAND(ID_ODBC_CREATE, OnOdbcCreate)
  32.     //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // COdbc2View construction/destruction
  37.  
  38. COdbc2View::COdbc2View()
  39. {
  40.     // TODO: add construction code here
  41.     m_pRecordset= NULL;
  42.     m_pRecordset2= NULL;
  43.     m_pRecordset3= NULL;
  44.  
  45. }
  46.  
  47. COdbc2View::~COdbc2View()
  48. {
  49. }
  50.  
  51. BOOL COdbc2View::PreCreateWindow(CREATESTRUCT& cs)
  52. {
  53.     // TODO: Modify the Window class or styles here by modifying
  54.     //  the CREATESTRUCT cs
  55.  
  56.     return CScrollView::PreCreateWindow(cs);
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // COdbc2View drawing
  61.  
  62. void COdbc2View::OnDraw(CDC* pDC)
  63. {
  64.     COdbc2Doc* pDoc = GetDocument();
  65.     ASSERT_VALID(pDoc);
  66.  
  67.     // TODO: add draw code for native data here
  68. }
  69.  
  70. void COdbc2View::OnInitialUpdate()
  71. {
  72.     CScrollView::OnInitialUpdate();
  73.     CSize sizeTotal;
  74.     // TODO: calculate the total size of this view
  75.     sizeTotal.cx = sizeTotal.cy = 100;
  76.     SetScrollSizes(MM_TEXT, sizeTotal);
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // COdbc2View diagnostics
  81.  
  82. #ifdef _DEBUG
  83. void COdbc2View::AssertValid() const
  84. {
  85.     CScrollView::AssertValid();
  86. }
  87.  
  88. void COdbc2View::Dump(CDumpContext& dc) const
  89. {
  90.     CScrollView::Dump(dc);
  91. }
  92.  
  93. COdbc2Doc* COdbc2View::GetDocument() // non-debug version is inline
  94. {
  95.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COdbc2Doc)));
  96.     return (COdbc2Doc*)m_pDocument;
  97. }
  98. #endif //_DEBUG
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // COdbc2View message handlers
  102. #include <ODBCINST.H>
  103. void COdbc2View::OnOdbcAdd() 
  104. {
  105.  
  106.     // Add an ODBC datasource to registry.
  107.     if (FALSE==::SQLConfigDataSource(
  108.         NULL, ODBC_ADD_DSN, "Microsoft Access Driver (*.mdb)",
  109.         "DSN=TestDataSource\0UID=jack\0PWD=password\0DBQ=c:\\Empty2.mdb\0")) 
  110.     {
  111.         AfxMessageBox("SQLConfigDataSource() returns FALSE");
  112.     }
  113.     
  114. }
  115.  
  116.  
  117. void COdbc2View::OnOdbcOpen() 
  118. {
  119.     // m_db is already constructed.
  120.     if (FALSE == m_db.Open("TestDataSource"))
  121.     {
  122.         AfxMessageBox("CDatabase.Open() returns FALSE");
  123.         return;
  124.     }
  125.     m_pRecordset= new CMyRecordset(&m_db);
  126.  
  127.     if (FALSE ==m_pRecordset->Open())
  128.     {
  129.         AfxMessageBox("CRecordset.Open() returns FALSE");
  130.         return;
  131.     }
  132.     while (!m_pRecordset->IsEOF())
  133.     {
  134.         CDBVariant var;
  135.         m_pRecordset->GetFieldValue("ID", var);
  136.  
  137.         m_pRecordset->MoveNext();
  138.     }
  139.  
  140.     m_pRecordset->Close();
  141.     m_db.Close();
  142.     delete m_pRecordset;
  143. }
  144.  
  145. void COdbc2View::OnOdbcGeneric() 
  146. {
  147.     // Add an ODBC datasource to registry.
  148.     if (FALSE==::SQLConfigDataSource(
  149.         NULL, ODBC_ADD_DSN, "Microsoft Access Driver (*.mdb)",
  150.         "DSN=GenericAccess\0UID=abc\0PWD=password\0")) 
  151.     {
  152.         AfxMessageBox("SQLConfigDataSource() returns FALSE");
  153.     }
  154.  
  155.     CDatabase db;
  156.     if (FALSE == db.Open("GenericAccess"))
  157.     {
  158.         AfxMessageBox("db.Open() returns FALSE");
  159.         return;
  160.     }
  161.     
  162. }
  163.  
  164. void COdbc2View::OnOdbcRowset() 
  165. {
  166.  
  167.     // m_db is already constructed.
  168.     if (FALSE == m_db.Open("Rowset"))
  169.     {
  170.         AfxMessageBox("CDatabase.Open() returns FALSE");
  171.         return;
  172.     }
  173.     m_pRecordset2= new CMyRecordset2(&m_db);
  174.  
  175.     // 
  176.     m_pRecordset2->SetRowsetSize(20);
  177.  
  178.     if (FALSE ==m_pRecordset2->Open(
  179.         AFX_DB_USE_DEFAULT_TYPE,
  180.         NULL,
  181.         CRecordset::useMultiRowFetch ))
  182.     {
  183.         AfxMessageBox("CRecordset2.Open() returns FALSE");
  184.         return;
  185.     }
  186.     while (!m_pRecordset2->IsEOF())
  187.     {
  188.         DWORD rows= m_pRecordset2->GetRowsFetched();
  189.         for (DWORD idx=0; idx<rows; idx++)
  190.         {
  191.             m_pRecordset2->SetRowsetCursorPosition(idx);
  192.             CDBVariant var;
  193.             m_pRecordset2->GetFieldValue("description", var);
  194.             if (var.m_dwType == DBVT_STRING)
  195.             {
  196.                 TRACE("desc: %s\n", *var.m_pstring);
  197.             }
  198.         }
  199.         m_pRecordset2->MoveNext();
  200.     }
  201.  
  202.     m_pRecordset2->Close();
  203.     m_db.Close();
  204.     delete m_pRecordset2;
  205.     
  206. }
  207.  
  208. void COdbc2View::OnOdbcQuery() 
  209. {
  210.     // m_db is already constructed.
  211.     if (FALSE == m_db.Open("TestDataSource"))
  212.     {
  213.         AfxMessageBox("CDatabase.Open() returns FALSE");
  214.         return;
  215.     }
  216.     m_pRecordset3= new CMyRecordset3(&m_db);
  217.     m_pRecordset3->m_IDParam= 4;
  218.     if (FALSE ==m_pRecordset3->Open())
  219.     {
  220.         AfxMessageBox("CRecordset.Open() returns FALSE");
  221.         return;
  222.     }
  223.     while (!m_pRecordset3->IsEOF())
  224.     {
  225.         TRACE("%d\n", m_pRecordset3->m_ID);
  226.         m_pRecordset3->MoveNext();
  227.     }
  228.  
  229.     m_pRecordset3->Close();
  230.     m_db.Close();
  231.     delete m_pRecordset3;
  232. }
  233.  
  234. void COdbc2View::OnOdbcInfo() 
  235. {
  236.     // TODO: Add your command handler code here
  237.     m_db.Open("TestDataSource");
  238.  
  239.     char* buf[512];
  240.     short cnt;
  241.     RETCODE ret= ::SQLGetInfo(m_db.m_hdbc,SQL_DATABASE_NAME,buf,512,&cnt);
  242.     TRACE("info: [%s]\n", buf);
  243. }
  244.  
  245. void COdbc2View::OnOdbcCreate() 
  246. {
  247.     // TODO: Add your command handler code here
  248.     m_db.Open("TestDataSource");
  249. #if 0
  250.     m_pRecordset= new CMyRecordset(&m_db);
  251.     m_pRecordset->Open();
  252.     ::SQLGetTypeInfo(m_pRecordset->m_hstmt, SQL_DOUBLE);
  253.     m_pRecordset->Close();
  254.     m_db.Close();
  255.     delete m_pRecordset;
  256. #endif
  257.  
  258.     m_db.ExecuteSQL("CREATE TABLE Servers (Name TEXT(15), Hits LONG)");
  259.  
  260.     m_db.ExecuteSQL("ALTER TABLE Servers ADD Location LONG");
  261. }
  262.