This article was previously published under Q309047
For a Microsoft Visual Basic .NET version of this article, see 308499
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:
When you use ADO in the Microsoft .NET Framework with Component Object Model (COM) interop, ADO properties that formerly accepted either object references or string values now only support object references. As a result, when you try to set a property to a string value, an exception is thrown.
To set an ADO property that accepts either an object or a string to a string value in the .NET Framework, use the let_ methods that COM interop generates for imported assemblies. For sample code that demonstrates this, refer to the "More Information" section.
Create a new Windows Application project in Visual C# .NET.
Place a Button control on Form1, and change its Name property to btnTest.
In Solution Explorer window, right-click References, and then click Add Reference.
On the COM tab in the Add Reference dialog box, click Microsoft ActiveX Data Objects 2.x Library. Click Select to add this reference to the Selected Components section, and then click OK.
If no wrapper is found for the selected library, you receive a warning. Click Yes to generate a wrapper. The ADODB reference is added to the project references.
Switch to Form view, and double-click btnTest to add the click event handler. Add the following code to the handler:
String myConnString =
"Provider=SQLOLEDB.1;User ID=sa;password=sa;Initial Catalog=pubs;
String mySelectQuery = "SELECT * FROM Authors";
ADODB.Connection myConnection = new ADODB.Connection();
ADODB.Recordset myRecordset1 = new ADODB.Recordset();
ADODB.Recordset myRecordset2 = new ADODB.Recordset();
//Setting the ActiveConnection to a Connection object will work properly.
myRecordset1.ActiveConnection = myConnection;
//Setting the ActiveConnection to a Connection string will fail.
//Comment the next line of code, and uncomment the line that uses
//the let_ActiveConnection to make the code work properly.
myRecordset2.ActiveConnection = myConnString;
catch (Exception ex)
Modify the connection string (myConnString) as appropriate for your environment.
Save your project.
On the Debug menu, click Start to run your project.
Click the button. A message box displays the result from the first Recordset object. This indicates that the assignment goes through successfully when you set the ActiveConnection property of the Recordset object to another object (in this case, the Connection object).
Set the ActiveConnection property of the Recordset to a string. Notice that the code fails with the above-mentioned exception.
Close the exception dialog box, and stop running the program.
Comment out the line that sets the ActiveConnection property to a Connection string. Uncomment the line that uses the let_ActiveConnection method.
Run the project again. Notice that the data is returned properly.