Solved

C++ and Access

Posted on 2004-08-26
4
389 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

747 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

13 Experts available now in Live!

Get 1:1 Help Now