Added:
trunk/examples/CPP/7/ADO/main.cpp
Log:
Added: trunk/examples/CPP/7/ADO/main.cpp
===================================================================
--- trunk/examples/CPP/7/ADO/main.cpp 2006-07-18 16:12:41 UTC (rev 447)
+++ trunk/examples/CPP/7/ADO/main.cpp 2006-07-18 16:13:32 UTC (rev 448)
@@ -0,0 +1,140 @@
+#include <ole2.h>
+#include <stdio.h>
+#include <conio.h>
+
+/*!
+ \note
+
+ Import ADO.
+
+ We can avoid using the full path if LIBPATH environment variable includes
+ the path to msado<VERSION>.dll.
+*/
+/*
+#import "msado15.dll" \
+ no_namespace rename("EOF", "EndOfFile")
+*/
+#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
+ no_namespace \
+ rename( "EOF", "EndOfFile")
+
+inline void TESTHR( HRESULT x ) { if FAILED(x) _com_issue_error(x); };
+
+_ConnectionPtr pConnection = NULL;
+char *pszConnectionString = "NorthwindMySQL";
+char *pszUserID = "myodbctest";
+char *pszPassword = "myodbctest";
+
+void doPrintProviderError( _ConnectionPtr pConnection )
+{
+ // Print Provider Errors from Connection object.
+ // pErr is a record object in the Connection's Error collection.
+ ErrorPtr pErr = NULL;
+
+ if( (pConnection->Errors->Count) > 0)
+ {
+ long nCount = pConnection->Errors->Count;
+
+ // Collection ranges from 0 to nCount -1.
+ for(long i = 0; i < nCount; i++)
+ {
+ pErr = pConnection->Errors->GetItem(i);
+ printf("Error number: %x\t%s\n", pErr->Number,
(LPCSTR)pErr->Description);
+ }
+ }
+}
+
+void doPrintComError( _com_error &e )
+{
+ _bstr_t bstrSource(e.Source());
+ _bstr_t bstrDescription(e.Description());
+
+ // Print Com errors.
+ printf("Error\n");
+ printf("\tCode = %08lx\n", e.Error());
+ printf("\tCode meaning = %s\n", e.ErrorMessage());
+ printf("\tSource = %s\n", (LPCSTR) bstrSource);
+ printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
+}
+
+bool doConnect()
+{
+ try
+ {
+ TESTHR( pConnection.CreateInstance( __uuidof( Connection ) ) );
+ pConnection->Open( pszConnectionString, pszUserID, pszPassword,
adConnectUnspecified );
+ printf( "[%s][%d] state: %d\n", __FILE__, __LINE__, pConnection->State );
+ }
+ catch ( _com_error &e )
+ {
+ doPrintProviderError( pConnection );
+ doPrintComError( e );
+ return false;
+ }
+
+ return true;
+}
+
+bool doSelect()
+{
+ _CommandPtr pCommand = NULL;
+
+ try
+ {
+ TESTHR( pCommand.CreateInstance( __uuidof( Command ) ) );
+ pCommand->CommandText = "SELECT * FROM orders WHERE EmployeeID = ?";
+ pCommand->CommandType = adCmdTable;
+ }
+ catch ( _com_error &e )
+ {
+ doPrintProviderError( pConnection );
+ doPrintComError( e );
+ return false;
+ }
+
+ return true;
+}
+
+bool doDisconnect()
+{
+ try
+ {
+ pConnection->Close();
+ printf( "[%s][%d] state: %d\n", __FILE__, __LINE__, pConnection->State );
+ }
+ catch ( _com_error &e )
+ {
+ doPrintProviderError( pConnection );
+ doPrintComError( e );
+ return false;
+ }
+
+ return true;
+}
+
+/*!
+ \brief main
+
+ This is the 'main' entry point to this program - it all starts here.
+*/
+void main()
+{
+ /*!
+ \note
+ */
+ if ( FAILED( ::CoInitialize( NULL ) ) )
+ return;
+
+ /* do it */
+ if ( doConnect() )
+ {
+ doSelect();
+ doDisconnect();
+ }
+
+ /*!
+ \note
+ */
+ ::CoUninitialize();
+}
+
| Thread |
|---|
| • Connector/ODBC 5 commit: r448 - trunk/examples/CPP/7/ADO | pharvey | 18 Jul |