This article was previously published under Q308047
This article has been archived. It is offered "as is" and will no longer be updated.
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
Create ADO Connection and Recordset Objects in Visual Basic .NET
Create a new Visual Basic .NET Windows Application project. Form1 is created by default.
From the Project menu, click Add Reference.
On the COM tab, click Microsoft ActiveX Data Objects 2.x Library, and then click Select.
Add the following code to the General Declarations section of Form1:
Dim cn As New ADODB.Connection()Dim rs As New ADODB.Recordset()Dim cnStr As StringDim cmd As New ADODB.Command()
The following code illustrates two different methods to open an ADO Connection object. Each of these methods connect to a Microsoft SQL Server Pubs database and can be added to the Form1 Load event.
Note User ID <user name> must have permissions to perform these operations on the database.
' Modify this connection string to reflect your server and logon information.' Store the connection to a variable to be used throughout this example.cnStr = "Provider=SQLOLEDB;Initial Catalog=Pubs;Data Source=servername;" & _ "User ID=<username>;Password=<strong password>;" ' 1. Connect through the Connectionstring property.cn.ConnectionString = cnStrcn.Open()cn.Close()' 2. Connect through the Connection object's Open method.cn.Open(cnStr)cn.Close()
The following code illustrates three different ways to open an ADO Recordset object. Each of these methods connect to a Microsoft SQL Server Pubs database and can be added to the Form1 Load event:
' 1. Open Recordset through the Execute method of the Connection object.cn.Open(cnStr)rs = cn.Execute("Select * from Authors")rs.Close()cn.Close()' 2. Open Recordset through the Command.Execute method.cn.Open(cnStr)cmd.ActiveConnection = cncmd.CommandText = "Select * from Authors"rs = cmd.Executers.Close()cn.Close()' 3. Open Recordset without a Connection object.rs.Open("Select * from Authors", cnStr)rs.Close()' Release the objects to free memory.rs = Nothingcn = Nothing
Modify the Connection string where indicated to properly connect to your SQL Server.
Press the F11 key to step through the code, and notice the different ways to create a Connection or Recordset object.
NOTE: When you set the Recordset object's ActiveConnection property to an actual string (as opposed to a Connection object), as follows