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
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.
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 <user name> must have the appropriate permissions to perform these operations on the database.
Dim cn As New ADODB.Connection()cn.ConnectionString = "provider=sqloledb;server=localhost;database=northwind;uid=<username>"cn.Open()
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.adUseClientrs.CursorType = ADODB.CursorTypeEnum.adOpenStaticrs.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.
Open a RecordSet by passing in an ad hoc SQL statement and the connection object:
rs.Open("select * from products", cn)
Disconnect the RecordSet, and then close out the connection:
rs.ActiveConnection = Nothingcn.Close()
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, "products")
Finally, add the last two lines of code to write the total number of rows in the DataSet to the console:
Console.Write("There are " & ds.Tables(0).Rows.Count.ToString & " total products.")Console.ReadLine()
Module Module1 Sub Main() Dim cn As New ADODB.Connection() cn.ConnectionString = "provider=sqloledb;server=(localhost);database=northwind;uid=<username>" 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("select * from products", cn) rs.ActiveConnection = Nothing cn.Close() Dim da As New System.Data.OleDb.OleDbDataAdapter() Dim ds As New DataSet() da.Fill(ds, rs, "products") Console.Write("There are " & ds.Tables(0).Rows.Count.ToString & " total products.") Console.ReadLine() End SubEnd Module
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.