How to use ADO RecordSet objects in Visual C++ 2005 or in Visual C++ .NET (816158)
The information in this article applies to:
- Microsoft ADO.NET (included with the .NET Framework) 1.0
- Microsoft ADO.NET (included with the .NET Framework 1.1)
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base: 840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application For a Microsoft Visual Basic .NET version of this
article, see
315974. For a Microsoft Visual C# .NET version of this
article, see
816159. This article refers to the following
Microsoft .NET Framework Class Library namespaces:
- System.Data
- System::Xml
- System::Data::OleDb
IN THIS TASKSUMMARYThis article describes how to create a console
application that uses COM Interop to create an earlier version of an ADO RecordSet object, to convert the RecordSet object to an ADO.NET DataSet object, and then
to display the record count. back to the topRequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
- Microsoft Windows 2000 Professional, Microsoft Windows 2000
Server, Microsoft Windows Server 2003, or Microsoft Windows XP
- The Microsoft .NET Framework
This article assumes that you are familiar with the following topics: - A general familiarity with Microsoft ADO and Microsoft ADO.NET
back to the topUse COM Components from Visual Studio .NET- Start Visual Studio .NET or Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- In Visual Studio .NET
2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.
In Visual Studio .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.
Note In Visual Stuio 2005, click Visual C++ under Project Types, and then click Console Application under Templates. - Type Q816158 in the
Name text box, type C:\ in
the Location text box, and then click
OK.
- When the project is created, make sure that Solution Explorer is visible. If it is not visible, press the CTRL + ALT + L key combination.
- Before you add code to the main() function, add a reference to
the Component Object Model (COM) component that you will use. In
Solution Explorer, right-click References under
Q816158, and then click Add Reference.
For additional information about adding references in Visual C++ .NET 2002, click the following article number to view the article in the Microsoft Knowledge Base:
310674
HOW TO: Add References to a Managed Visual C++ Project
- On the
COM tab in the Add Reference dialog box, click Microsoft ActiveX Data Objects 2.5
Library, and then click Select. Your selection appears under Selected Components. Click
OK. ADODB appears under
References in the Q816158 project.
Note In Visual Studio 2005, you do not have to click Select. - In Solution Explorer, right-click
References under Q816158, and then click
Add Reference.
- On the
.NET tab in the Add Reference dialog box, click the System.Data.dll component and the
System.Xml.dll component. Click Select. The components appear under Selected Components.
Click OK. System.Data and
System.Xml appear under References in
the Q816158 project.
Note In Visual Studio 2005, you do not have to click Select. - If the Q816158.cpp file is not open in the editor window double-click Q816158.cpp in Solution Explorer. Now that you have a reference to an earlier version of an ADO component,
its full capabilities are at your disposal. Additionally, Visual Studio .NET
provides full Microsoft IntelliSense support for COM objects.
- Add the following namespaces to the Q816158.cpp file:
using namespace System::Data;
using namespace System::Data::OleDb; Note The System.Data namespace consists mostly of the classes that constitute the
ADO.NET architecture. You can build components
that efficiently manage data from multiple data sources with the ADO.NET architecture. In a disconnected
scenario (such as the Internet), ADO.NET provides the tools to request, to update,
and to reconcile data in multiple-tier systems.
You can access a data
source to use in conjunction with a DataSet with the System.Data.OleDb namespace (the OLE DB .NET Data Provider). - The first few lines of code create and open a connection.
Replace the existing code in the main() function with the following code:
ADODB::ConnectionClass* cnn = new ADODB::ConnectionClass();
cnn->ConnectionString = S"provider=SQLOLEDB.1;Data Source=<your SQL server>;Initial Catalog=Northwind;User Id=<username>;Password=<password>";
cnn->Open(S"",S"",S"",-1); Note Set the values
for Data Source, User ID, and Password in the ConnectionString as appropriate for your environment . - Create an instance of an ADO RecordSet object. Set the
cursor and the lock properties. To do this, add the following code to the existing code:
ADODB::RecordsetClass *rs = new ADODB::RecordsetClass();
rs->CursorLocation = ADODB::CursorLocationEnum::adUseClient;
Note This code will be familiar to you if you have experience with earlier versions of ADO. The difference is that you are now working with it in
.NET. - Open a RecordSet object by passing in the following SQL statement that was created for this specific case, and pass in the connection object:
rs->Open(S"select * from Products", cnn,ADODB::CursorTypeEnum::adOpenStatic,ADODB::LockTypeEnum::adLockBatchOptimistic,-1); - Disconnect the RecordSet object, and then close the connection as follows:
rs->ActiveConnection = NULL;
cnn->Close();
- To make the RecordSet object fully usable in a .NET
application, convert the RecordSet to an ADO.NET DataSet object by using the OleDbDataAdapter class:
OleDbDataAdapter *da = new OleDbDataAdapter();
DataSet *ds = new DataSet();
da->Fill(ds,rs,S"products");
- Add the last two lines of code as follows to write the total
number of rows in the DataSet object to the console:
Console::WriteLine(S"There are {0} total products.", ds->Tables->get_Item(0)->Rows->Count.ToString());
Console::ReadLine();
back to the topComplete Code Listing#using <mscorlib.dll>
using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;
int _tmain()
{
try
{
ADODB::ConnectionClass* cnn = new ADODB::ConnectionClass();
cnn->ConnectionString = S"provider=SQLOLEDB.1;Data Source=<your SQL server>;Initial Catalog=Northwind;User Id=<username>;Password=<password>";
cnn->Open(S"",S"",S"",-1);
ADODB::RecordsetClass *rs = new ADODB::RecordsetClass();
rs->CursorLocation = ADODB::CursorLocationEnum::adUseClient;
rs->Open(S"select * from Products", cnn,ADODB::CursorTypeEnum::adOpenStatic,ADODB::LockTypeEnum::adLockBatchOptimistic,-1);
rs->ActiveConnection = NULL;
cnn->Close();
OleDbDataAdapter *da = new OleDbDataAdapter();
DataSet *ds = new DataSet();
da->Fill(ds,rs,S"products");
Console::WriteLine(S"There are {0} total products.", ds->Tables->get_Item(0)->Rows->Count.ToString());
Console::ReadLine();
}
catch(Exception *ex)
{
Console::WriteLine(ex->Message );
}
return 0;
} Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: back to the topVerify That It Works- Press F5 to run the application in debug mode.
- After a brief pause you see the
following:There are 77 total
products.
- Press ENTER to quit the console application and to return to
Visual Studio .NET.
back to the topTroubleshootingYou may have to modify the connection string to run this
application (specifically the server name). Although provider=sqloledb is not required for .NET applications, in this case you must have
it because .NET uses ODBC for earlier versions of ADO. back to the topREFERENCESFor more information about exposing COM Components to the
.NET Framework, visit the following Microsoft Developer Network (MSDN) Web site: For
more information about advanced COM Interop, visit the following MSDN Web
site: For
more information about data namespaces in Visual Studio, visit the following
MSDN Web site: back to the top
Modification Type: | Major | Last Reviewed: | 6/15/2006 |
---|
Keywords: | kbinterop kbConsole kbCOMInterop kbDatabase kbDataAdapter kbHOWTOmaster KB816158 kbAudDeveloper kbAudITPRO |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|