Pages

ADO Recordset vs. ADO.NET


.NET provides a wide range of controls for both Web applications and Microsoft Windows–based form applications. Many of these controls support data binding with ADO.NET data sources. The question is how to take advantage of your existing ADO infrastructure and still use the new controls. The answer is simple: all you need to do is convert your Recordset to either a DataSet or a Data Table. Once you have one of these managed data objects, data binding is trivial.


‘Here is the familiar ADO stuff

Dim cn As New ADODB.Connection()

Dim rs As New ADODB.Recordset()

cn.Open(“Provider=SQLOLEDB.1;Integrated Security=SSPI;” & _

“Persist Security Info=False;” & _

“Initial Catalog=Northwind;Data Source=bart”)

rs.Open(“Select * From Employees”, cn)

‘This is where things start getting interesting. Here we declare not

‘only the DataAdapter, but also the DataTable the data is destined for.

Dim olead As New Data.OleDb.OleDbDataAdapter()

Dim dt As New Data.DataTable()

‘We use the DataAdapter to fill a DataTable

olead.Fill(dt, rs)

‘Now we bind the DataTable to the DataGrid

Me.DataGrid1.DataSource = dt

‘This is cleanup. Always close your objects as

’soon as you are done with them.

rs.Close()

cn.Close()

In this simple example, an ADO Recordset is bound to a DataGrid control on a Windows form.

The OleDbDataAdapter.Fill method is capable of populating a DataTable object with data from an ADODB Recordset.

In this way it is possible to take any Recordset and convert it to a managed data structure. Once you have done the conversion, you can bind the DataTable directly to a control,

or you can add it to a DataSet and work with the table just as you would any other DataTable (including creating a DataView for the data, which we will get to a bit later on).

0 comments:

Post a Comment