home *** CD-ROM | disk | FTP | other *** search
- // odbc2View.cpp : implementation of the COdbc2View class
- //
-
- #include "stdafx.h"
- #include "odbc2.h"
-
- #include "odbc2Doc.h"
- #include "MyRecordset2.h"
- #include "odbc2View.h"
-
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // COdbc2View
-
- IMPLEMENT_DYNCREATE(COdbc2View, CScrollView)
-
- BEGIN_MESSAGE_MAP(COdbc2View, CScrollView)
- //{{AFX_MSG_MAP(COdbc2View)
- ON_COMMAND(ID_ODBC_ADD, OnOdbcAdd)
- ON_COMMAND(ID_ODBC_OPEN, OnOdbcOpen)
- ON_COMMAND(ID_ODBC_GENERIC, OnOdbcGeneric)
- ON_COMMAND(ID_ODBC_ROWSET, OnOdbcRowset)
- ON_COMMAND(ID_ODBC_QUERY, OnOdbcQuery)
- ON_COMMAND(ID_ODBC_INFO, OnOdbcInfo)
- ON_COMMAND(ID_ODBC_CREATE, OnOdbcCreate)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // COdbc2View construction/destruction
-
- COdbc2View::COdbc2View()
- {
- // TODO: add construction code here
- m_pRecordset= NULL;
- m_pRecordset2= NULL;
- m_pRecordset3= NULL;
-
- }
-
- COdbc2View::~COdbc2View()
- {
- }
-
- BOOL COdbc2View::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CScrollView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // COdbc2View drawing
-
- void COdbc2View::OnDraw(CDC* pDC)
- {
- COdbc2Doc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
-
- // TODO: add draw code for native data here
- }
-
- void COdbc2View::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
- CSize sizeTotal;
- // TODO: calculate the total size of this view
- sizeTotal.cx = sizeTotal.cy = 100;
- SetScrollSizes(MM_TEXT, sizeTotal);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // COdbc2View diagnostics
-
- #ifdef _DEBUG
- void COdbc2View::AssertValid() const
- {
- CScrollView::AssertValid();
- }
-
- void COdbc2View::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
-
- COdbc2Doc* COdbc2View::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COdbc2Doc)));
- return (COdbc2Doc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // COdbc2View message handlers
- #include <ODBCINST.H>
- void COdbc2View::OnOdbcAdd()
- {
-
- // Add an ODBC datasource to registry.
- if (FALSE==::SQLConfigDataSource(
- NULL, ODBC_ADD_DSN, "Microsoft Access Driver (*.mdb)",
- "DSN=TestDataSource\0UID=jack\0PWD=password\0DBQ=c:\\Empty2.mdb\0"))
- {
- AfxMessageBox("SQLConfigDataSource() returns FALSE");
- }
-
- }
-
-
- void COdbc2View::OnOdbcOpen()
- {
- // m_db is already constructed.
- if (FALSE == m_db.Open("TestDataSource"))
- {
- AfxMessageBox("CDatabase.Open() returns FALSE");
- return;
- }
- m_pRecordset= new CMyRecordset(&m_db);
-
- if (FALSE ==m_pRecordset->Open())
- {
- AfxMessageBox("CRecordset.Open() returns FALSE");
- return;
- }
- while (!m_pRecordset->IsEOF())
- {
- CDBVariant var;
- m_pRecordset->GetFieldValue("ID", var);
-
- m_pRecordset->MoveNext();
- }
-
- m_pRecordset->Close();
- m_db.Close();
- delete m_pRecordset;
- }
-
- void COdbc2View::OnOdbcGeneric()
- {
- // Add an ODBC datasource to registry.
- if (FALSE==::SQLConfigDataSource(
- NULL, ODBC_ADD_DSN, "Microsoft Access Driver (*.mdb)",
- "DSN=GenericAccess\0UID=abc\0PWD=password\0"))
- {
- AfxMessageBox("SQLConfigDataSource() returns FALSE");
- }
-
- CDatabase db;
- if (FALSE == db.Open("GenericAccess"))
- {
- AfxMessageBox("db.Open() returns FALSE");
- return;
- }
-
- }
-
- void COdbc2View::OnOdbcRowset()
- {
-
- // m_db is already constructed.
- if (FALSE == m_db.Open("Rowset"))
- {
- AfxMessageBox("CDatabase.Open() returns FALSE");
- return;
- }
- m_pRecordset2= new CMyRecordset2(&m_db);
-
- //
- m_pRecordset2->SetRowsetSize(20);
-
- if (FALSE ==m_pRecordset2->Open(
- AFX_DB_USE_DEFAULT_TYPE,
- NULL,
- CRecordset::useMultiRowFetch ))
- {
- AfxMessageBox("CRecordset2.Open() returns FALSE");
- return;
- }
- while (!m_pRecordset2->IsEOF())
- {
- DWORD rows= m_pRecordset2->GetRowsFetched();
- for (DWORD idx=0; idx<rows; idx++)
- {
- m_pRecordset2->SetRowsetCursorPosition(idx);
- CDBVariant var;
- m_pRecordset2->GetFieldValue("description", var);
- if (var.m_dwType == DBVT_STRING)
- {
- TRACE("desc: %s\n", *var.m_pstring);
- }
- }
- m_pRecordset2->MoveNext();
- }
-
- m_pRecordset2->Close();
- m_db.Close();
- delete m_pRecordset2;
-
- }
-
- void COdbc2View::OnOdbcQuery()
- {
- // m_db is already constructed.
- if (FALSE == m_db.Open("TestDataSource"))
- {
- AfxMessageBox("CDatabase.Open() returns FALSE");
- return;
- }
- m_pRecordset3= new CMyRecordset3(&m_db);
- m_pRecordset3->m_IDParam= 4;
- if (FALSE ==m_pRecordset3->Open())
- {
- AfxMessageBox("CRecordset.Open() returns FALSE");
- return;
- }
- while (!m_pRecordset3->IsEOF())
- {
- TRACE("%d\n", m_pRecordset3->m_ID);
- m_pRecordset3->MoveNext();
- }
-
- m_pRecordset3->Close();
- m_db.Close();
- delete m_pRecordset3;
- }
-
- void COdbc2View::OnOdbcInfo()
- {
- // TODO: Add your command handler code here
- m_db.Open("TestDataSource");
-
- char* buf[512];
- short cnt;
- RETCODE ret= ::SQLGetInfo(m_db.m_hdbc,SQL_DATABASE_NAME,buf,512,&cnt);
- TRACE("info: [%s]\n", buf);
- }
-
- void COdbc2View::OnOdbcCreate()
- {
- // TODO: Add your command handler code here
- m_db.Open("TestDataSource");
- #if 0
- m_pRecordset= new CMyRecordset(&m_db);
- m_pRecordset->Open();
- ::SQLGetTypeInfo(m_pRecordset->m_hstmt, SQL_DOUBLE);
- m_pRecordset->Close();
- m_db.Close();
- delete m_pRecordset;
- #endif
-
- m_db.ExecuteSQL("CREATE TABLE Servers (Name TEXT(15), Hits LONG)");
-
- m_db.ExecuteSQL("ALTER TABLE Servers ADD Location LONG");
- }
-