Microsoft KB Archive/308611

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

Article ID: 308611

Article Last Modified on 11/28/2007

-

APPLIES TO


 * 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

-



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.

IN THIS TASK
SUMMARY
 * Create ADO Connection and Recordset Objects in C#.NET

REFERENCES



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

back to the top

Create ADO Connection and Recordset Objects in Visual C# .NET
 Create a new Visual C# .NET Windows application project. On the Project menu, click Add Reference. Click the COM tab. Click Microsoft ActiveX Data Objects 2.X Library. Open the Code window for Form1.  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;   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  must have permissions to perform these operations on the database. //Connection string. cnStr = &quot;Provider=SQLOLEDB;Initial Catalog=Pubs;Data Source=servername;User ID= ;Password=; &quot;; //query query = &quot;Select * From Authors&quot;;

//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; </li> 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.</li></ol>

back to the top

<div class="references_section">