You are currently offline, waiting for your internet to reconnect

HOW TO: Open ADO Connection and RecordSet Objects in Visual C# .NET

This article was previously published under Q308611
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 308047.


This step-by-step article shows you how to create ActiveX Data Objects (ADO) Connection and Recordset objects in Visual C# .NET.

Create ADO Connection and Recordset Objects in Visual C# .NET

  1. Create a new Visual C# .NET Windows application project.
  2. On the Project menu, click Add Reference.
  3. Click the COM tab. Click Microsoft ActiveX Data Objects 2.X Library.
  4. Open the Code window for Form1.
  5. So that your declarations will be form-level in scope, add the following code to the top of the Form1 class section, above the public Form1 function:
         private ADODB.Connection cn = new ADODB.Connection();     private ADODB.Recordset rs = new ADODB.Recordset();     private string cnStr;     private string query;					
  6. Use the following code samples in the Form1 function below InitializeComponent();. The first example creates a Connection object and connects to a SQL Server Pubs database. The second example creates a RecordSet object.

    To connect to the database:

    Note User ID <user name> must have permissions to perform these operations on the database.
         //Connection string.     cnStr = "Provider=SQLOLEDB;Initial Catalog=Pubs;Data Source=servername;User ID=<username>;Password=;<strong password>";     //query     query = "Select * From Authors";     //Connection via Connection open Property.     cn.Open(cnStr, null, null, 0);     cn.Close();      //Connection via ConnectionString Property.     cn.ConnectionString = cnStr;     cn.Open(null, null, null, 0);     cn.Close();    						
    To retrieve the RecordSet from database:
         //Open Recordset via Connection object.     cn.Open(cnStr, null, null, 0);     rs.Open(query, cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, -1);     rs.Close();     cn.Close();      //Open Recordset without connection object.     rs.Open(query, cnStr, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, -1);		     rs.Close(); 					
  7. Modify the ConnectionString object for your SQL Server. Press F11 to step through the code and note the different ways to create a Connection or RecordSet object.

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
168336 HOWTO: Open ADO Connection and Recordset Objects

Article ID: 308611 - Last Review: 11/28/2007 18:04:23 - Revision: 4.2

  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7
  • kbadonet kbhowtomaster kbsample KB308611