Click here to Skip to main content
11,790,613 members (62,111 online)
Click here to Skip to main content

Using ADO.NET in a managed C++ application

, 11 Sep 2001 101.5K 2K 22
Rate this:
Please Sign up or sign in to vote.
This article demonstrates the basics of accessing a database and retrieving data from that database usingthe ADO.NET data classes.
<!-- Download Links --> <!-- Main HTML starts here -->


This article demonstrates the basics of accessing a database and retrieving data from that database usingthe ADO.NET data classes.

It uses the .NET ADODataReader class to retrieve a read-only, forward-only stream of data from the database. Using the Data Reader can increase application performance and reduce system overhead becauseonly one row at a time is ever in memory.

This example is based on the standard Managed C++ Application created by the Application Wizard.

To get an access to the .NET Framework classes we need for database support, we should add the following lines to the main source file:

#using <mscorlib.dll>

// Add access to .NET Framework classes.
#using <System.dll>
#using <System.Data.dll>

using namespace System;
using namespace System::Data::ADO;

To retrieve the data from the database we must first create a connection to the database using the ADOConnection class. We then set the ConnectionString property to specify the data source and then connect to the database using the Open() method of the ADOConnection class.

After that we create a command object to get the data using the ADOCommand class. Executing this command returns us a reference to the data reader class, which is an instance of the ADODataReader class.

We then loop through each row of data one at the time by calling the Read() member of the ADODataReader class. It retrieves data that is accessible as an items collection. We can get items by index or by column name. Note: We should always call the Read method before we access the data in the ADODataReader object.

int main(void)
    OleDbConnection* connection;    // ADO connection.
    OleDbCommand* command;            // ADO command
    OleDbDataReader* dataReader;    // ADO data reader

        // Create connection, set connection string and open connection to
        // specified database.
        connection = new OleDbConnection();
                                         "Data Source=..\\Data\\grocertogo.mdb;"
                                         "Persist Security Info=False");

        // Create command and get data reader by executing this command.
        command = new OleDbCommand(S"SELECT ProductName, UnitPrice FROM Products", 
        dataReader = command->ExecuteReader();

        // Print table header
        Console::WriteLine(S"Product                       | Price");

        // Iterate through rows set and print data.
            Console::WriteLine(S"{0, -30}| {1}", dataReader->get_Item("ProductName"), 

        // Print table footer.

        // Close DataReader
        // Close connection.
    catch(Exception* e)
        // Print error message and close connection.
        Console::WriteLine("Error occured: {0}", e->Message);
        if (dataReader && !dataReader->IsClosed) 
        if (connection->State == ConnectionState::Open) 

    Console::WriteLine("\n\n -- PROGRAM ENDS --\nPress Enter to continue");

    return 0;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Igor Chouvalov
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralProblems when compiling Pin
mjbari15-Apr-06 0:29
membermjbari15-Apr-06 0:29 
GeneralYes but.. Pin
Anonymous24-Jun-03 12:18
sussAnonymous24-Jun-03 12:18 
General.NET is slow slow slow Pin
Chadwick16-Jan-02 9:54
memberChadwick16-Jan-02 9:54 article Pin
Thomas Shokite21-Nov-01 5:13
memberThomas Shokite21-Nov-01 5:13 
GeneralRe: article Pin
Tapan.Net27-Apr-04 1:56
memberTapan.Net27-Apr-04 1:56 
Generalhello Pin
Anonymous9-Nov-01 19:52
memberAnonymous9-Nov-01 19:52 
GeneralRe: hello Pin
R.selvam2-Feb-04 6:31
memberR.selvam2-Feb-04 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1509028.1 | Last Updated 12 Sep 2001
Article Copyright 2001 by Igor Chouvalov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid