Using Ado.net in Managed C + + applications

Source: Internet
Author: User
Summary:

In this paper, we use the Ado.net data object to access the database and the basic principle of data fetching from the database to produce examples.

Body:

This article provides an example of a standards-based managed C + + application generated by the Application Wizard, which uses the. NET Adodatareader class retrieves a read-only (read-only), forward-only (forward-only) stream of data from the database. It is because in memory only one line at a time, the data reader can produce application execution and simplify the system socket (overhead). Add the following code to the main source file to gain access to the. NET framework classes that have database support:

#using

ADD access to. NET Framework classes.
#using
#using

using namespace System;
Using namespace System::D Ata::ado;
To retrieve data from the database, first generate a connection to the database with the ADOConnection class, then set the ConnectionString property to specify the data source, and connect to the database with the ADOConnection class's Open () method.

The Adocommand class is then used to generate a command object to get the data, and the Execute command returns a reference to a data reader class, that is, an instance of the Adodatareader class.

The read () member of the Adodatareader class is then iterated through all rows of data one line at a time, and the data obtained is as accessible as a project collection (Items collection), and we can get the items using index values or column names. Note: You should first call the Read method before accessing the data in the Adodatareader object.

int main (void)
{
adoconnection* connection; ADO connection.
adocommand* command; ADO command
adodatareader* DataReader; ADO Data reader

Try
{
Create connection, set connection string and open connection to
Specified database.
Connection = new ADOConnection ();
connection->connectionstring = S "Provider=Microsoft.Jet.OLEDB.4.0;Data source=. \\Data\\grocertogo.mdb; Persist security Info=false ";

Connection->open ();

Create command and get data reader by executing the This command.
Command = new Adocommand (S "Select ProductName, UnitPrice from Products", connection);
Command->execute (&datareader);

Print table Header
Console::WriteLine (S "_____________________________________");
Console::WriteLine (S) Product | Price ");
Console::WriteLine (S "_____________________________________");

Iterate through rows set and print data.
while (Datareader->read ())
Console::WriteLine (S "{0, -30}| {1} ", Datareader->get_item (" ProductName "), Datareader->get_item (" UnitPrice "));

Print table Footer.
Console::WriteLine (S "_____________________________________");

Close DataReader
Datareader->close ();
Close connection.
Connection->close ();
}
catch (exception* e)
{
Print error message and close connection.
Console::WriteLine ("Error occured: {0}", e->message);
if (DataReader &&!datareader->isclosed)
Datareader->close ();
if (connection->state = = Dbobjectstate::open)
Connection->close ();
}

Console::WriteLine ("Press ENTER to continue");
Console::readline ();

return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.