Microsoft KB Archive/308611

From BetaArchive Wiki

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

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

  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.

back to the top

REFERENCES

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


back to the top

Keywords: kbadonet kbhowtomaster kbsample KB308611