Solved

C++ and Access

Posted on 2004-08-26
4
395 Views
Last Modified: 2013-11-25
Hello all,

Is there a way to write to an Access 2000 database using straight C++?  I would like to do it without using MFC, just straight console.  Any and all help is greatly appreciated!  Thanks in advance!

Tony
0
Comment
Question by:fattumsdad
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Indrawati
ID: 11909824
Any reason why you don't want to use MFC? Since Access is made by Microsoft, I doubt you can easily manipulate access databases without MFC, unless you know the internal data structure of Access.
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11909930
Yes, it's very easy with OLEDB.  You can really go native and use the raw COM calls, but the ATL template OLEDB classes are a lot easier to use.  Here's some code to demonstrate how it works.  Similar to ODBC, this code will work against any database that has an OLEDB driver.  Here's a link to the docs, too:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/vcrefoledbtemplates.asp


Code:
---------------------------------
#include <atldbcli.h>
#include <atldbsch.h>

void PrintRowsetData(ATL::CAccessorRowset<CDynamicAccessor> &rs)
{
    HRESULT hr = rs.MoveFirst( );
    while( SUCCEEDED( hr ) && hr != DB_S_ENDOFROWSET )
    {
        for( size_t i = 1; i <= rs.GetColumnCount( ); i++ )
        {
            DBTYPE type;
            rs.GetColumnType( i, &type );
            printf( "Column %d [%S] is of type %d\n", i, rs.GetColumnName( i ), type );
               
            switch( type )
            {
            case DBTYPE_WSTR:
                printf( "value is %S\n", (WCHAR*)rs.GetValue( i ) );
                break;
            case DBTYPE_STR:
                printf( "value is %s\n", (CHAR*)rs.GetValue( i ) );
            default:
                printf( "value is %d\n", *(long*)rs.GetValue( i ) );
            }
        }
        hr = rs.MoveNext( );
    }
}

void OLEDBTest()
{
    ::CoInitialize(NULL);
    ATL::CDataSource ds;
    // This uses a UDL file for initialization parameters
    if ( ds.OpenFromFileName(OLESTR("F:\\MyDB.udl")) == S_OK )
    // Could also generate prompt for UDL file - same code would work against any OLE DB provider (like SQL Server)
    // if ( ds.OpenWithPromptFileName() == S_OK )
    {
        ATL::CSession session;
        if ( session.Open(ds) == S_OK )
        {
            // LIst the tables in the database
            ATL::CTables tables;
            tables.Open(session, NULL, NULL, NULL, "TABLE");
            char tableName[32];
            tableName[0] = 0;
            while (tables.MoveNext() == S_OK )
            {
                std::cout << "Table: " << tables.m_szName << std::endl;
                // Save first table name
                if ( tableName[0] == 0 ) ::strcpy(tableName, tables.m_szName);
            }

            // Show a table - use dynamic accessor to discover column names
            ATL::CTable<CDynamicAccessor> table;
            table.Open(session, tableName);
            PrintRowsetData(table);
            table.Close();

            std::cout << std::endl << "------------------------" << std::endl << std::endl;

            // Run a command, again with dynamic accessor
            ATL::CCommand<CDynamicAccessor> command;
            char cmdStr[64];
            // Column names hardcoded here (could just select names from table info above)
            ::sprintf(cmdStr, "SELECT Name, Phone FROM %s;", tableName);
            command.Open(session, cmdStr);
            PrintRowsetData(command);
            command.Close();

            session.Close();
        }
        ds.Close();
    }
    ::CoUninitialize();
}
0
 
LVL 19

Expert Comment

by:drichards
ID: 11915915
Just a note about the code as I look at it:  You'll need to create a udl file and change the code to open it rather than "F:\MyDB.udl".  Easiest way is to go into file explorer right pane and right click and do New->Text File and rename the file to <your name>.udl.  Then double-click on the empty udl file and you'll get a configuration dialog.  On the Provider tab, select "Microsoft Jet 4.0 OLE DB Provider".  If you're using Access prior to Acces 2000, use Jet 3.51 provider.

Then on the Connection tab select the Access file and test the connection.  That will get you going.

You'll also want to change the query for the CCommand to something that will work on your database.
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11916121
drichards,

I appreciate all the help!  Thanks a lot!!

Regards,
Tony
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SignTool Error with Windows 10 5 121
How can i compile this github project?? 2 75
Embarcadero WebBroker REST server endpoint parameters 2 54
gdb doesn't stop on breakpoint 2 50
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now