Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (12.59 KB, 2 trang )
[ Team LiB ]
Recipe 5.9 Loading an ADO Recordset into a DataSet
Problem
You want to convert an ADO Recordset generated within a legacy application to a
DataSet so that you can use it in a .NET application.
Solution
Use COM interop or the Fill( ) method of the OLE DB data provider DataAdapter.
You'll need a reference to the Primary Interop Assembly (PIA) for ADO provided in the
file ADODB.DLL. Select adodb from the .NET tab in Visual Studio .NET's Add
Reference Dialog.
The sample code creates an ADO Recordset for the Orders table in Northwind. The Fill( )
method of the OleDbDataAdapter is used to load the Recordset into a DataTable.
The C# code is shown in Example 5-9
.
Example 5-9. File: AdoRecordsetForm.cs
// Namespaces, variables, and constants
using System;
using System.Data;
using System.Data.OleDb;
// . . .
// Open an ADO connection.
ADODB.Connection conn = new ADODB.Connection( );
conn.Open("Provider = SQLOLEDB;Data Source = (local);" +
"Initial Catalog = northwind","sa","",0);
// Create an ADO recordset.
ADODB.Recordset rs = new ADODB.Recordset( );