Microsoft KB Archive/315974

= How to use ADO Recordsets in Visual Basic .NET =

Article ID: 315974

Article Last Modified on 11/28/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q315974





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

IN THIS TASK
SUMMARY
 * Requirements
 * Use COM Components from Visual Studio .NET
 * Complete Code Display (Module1.vb)
 * Verify That It Works
 * Troubleshooting

REFERENCES



SUMMARY
This article explains how to create a small console application that uses COM Interop to create an earlier version (legacy) ADO RecordSet, convert it to an ADO.NET DataSet, and then display the record count. You will see how easy it is to use COM components from within Microsoft Visual Studio .NET.

back to the top

Requirements
You need the following elements to perform the procedures in this article:
 * A Microsoft Windows 2000 Professional (or Windows 2000 Server) or Microsoft Windows XP Professional-based system with the .NET Framework installed.
 * A general familiarity with ADO and ADO.NET.

back to the top

Use COM components from Visual Studio .NET
 Start Visual Studio .NET. Click New Project, click Visual Basic Projects, and then select Console Application. Name the application ComDemo, and then click OK. When the Project is created, ensure that the Solution Explorer is visible. If it is not, press CTRL+ALT+L. Before you add code to Sub Main, add a reference to the COM component that you will be using. In the Solution Explorer, right-click References under ComDemo, and then click Add Reference. On the COM tab, select Microsoft ActiveX Data Objects 2.5 Library. Click Select. Your selection should be displayed in the Selected Components list box. Click OK. You should now see ADODB listed under References in the Web application. Module1.vb should be open in the editor window. If it is not, double-click this file in the Solution Explorer. Now that you have a reference to a legacy ADO component, its full capabilities are at your disposal. Additionally, Visual Studio .NET provides full IntelliSense support for COM objects.  The first few lines of code create and open a connection. Type the following immediately below the Sub Main line of the module:

Note Uid must have the appropriate permissions to perform these operations on the database.

Dim cn As New ADODB.Connection cn.ConnectionString = &quot;provider=sqloledb;server=localhost;database=northwind;uid= &quot; cn.Open </li>  Next, create an instance of an ADO RecordSet, setting the cursor and lock properties. To do this, add the following to the existing code: Dim rs As New ADODB.RecordSet rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic NOTE: This should be very familiar code if you have experience with legacy ADO. The difference is that you are now working with it from within .NET. </li>  Open a RecordSet by passing in an ad hoc SQL statement and the connection object: rs.Open(&quot;select * from products&quot;, cn) </li>  Disconnect the RecordSet, and then close out the connection: rs.ActiveConnection = Nothing cn.Close </li>  You now have a disconnected RecordSet. To make this a little more interesting, and to make the RecordSet fully usable within a .NET application, convert it to an ADO.NET DataSet by using the OleDbDataAdapter class: Dim da As New System.Data.OleDb.OleDbDataAdapter Dim ds As New DataSet da.Fill(ds, rs, &quot;products&quot;) </li>  Finally, add the last two lines of code to write the total number of rows in the DataSet to the console: Console.Write(&quot;There are &quot; & ds.Tables(0).Rows.Count.ToString & &quot; total products.&quot;) Console.ReadLine </li></ol>

back to the top

Complete code display (Module1.vb)
Module Module1

Sub Main Dim cn As New ADODB.Connection cn.ConnectionString = &quot;provider=sqloledb;server=(localhost);database=northwind;uid= &quot; cn.Open

Dim rs As New ADODB.RecordSet rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic rs.Open(&quot;select * from products&quot;, cn) rs.ActiveConnection = Nothing cn.Close

Dim da As New System.Data.OleDb.OleDbDataAdapter Dim ds As New DataSet da.Fill(ds, rs, &quot;products&quot;)

Console.Write(&quot;There are &quot; & ds.Tables(0).Rows.Count.ToString & &quot; total products.&quot;) Console.ReadLine End Sub

End Module back to the top

Verify that it works
<ol> Press F5 to run the application in debug mode.</li> After a brief pause you should see the following:

There are 77 total products.

NOTE: This number may vary if you have altered the Northwind database.</li> Press ENTER to exit the console application and return to Visual Studio .NET.</li></ol>

back to the top

Troubleshooting
You may have to modify the connection string to run this application -- specifically the server name. Also, although provider=sqloledb is not normally needed for .NET applications, in this case you do need it because .NET defaults to ODBC for legacy ADO.

back to the top

<div class="references_section">