1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Loading an ADO Recordset into a DataSet pdf

2 313 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Loading an ADO Recordset into a DataSet
Tác giả Team Lib
Thể loại Recipe
Định dạng
Số trang 2
Dung lượng 12,59 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

[ 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

Trang 1

[ 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( );

rs.Open("SELECT * FROM Orders", conn,

ADODB.CursorTypeEnum.adOpenForwardOnly,

ADODB.LockTypeEnum.adLockReadOnly, 0);

// Create and fill a dt from the ADO recordset

Trang 2

DataTable dt = new DataTable("Orders");

(new OleDbDataAdapter( )).Fill(dt, rs);

conn.Close( );

// Bind the default view of the dt to the grid

dataGrid.DataSource = dt.DefaultView;

Discussion

One of the overloads of the OLE DB NET DataAdapter.Fill( ) method accepts an ADO Recordset or Record object The COM component that returns an ADO Recordset or Record object is consumed using COM interop

While the data can be loaded into a DataSet in this way, there is no way to reconcile the changes made to the data in the DataSet with the data source underlying the ADO object This must be explicitly handled

There is no FillSchema( ) method which allows the schema of an ADO Recordset to be retrieved into a DataSet

[ Team LiB ]

Ngày đăng: 14/12/2013, 18:16

TỪ KHÓA LIÊN QUAN

w