tailieunhanh - Loading an ADO Recordset into a DataSet

[ Team LiB ] Recipe 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. | Team LiB Recipe 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 . 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 Namespaces variables and constants using System using using . . . Open an ADO connection. conn new Provider SQLOLEDB Data Source local Initial Catalog northwind sa 0 Create an ADO recordset. rs new SELECT FROM Orders conn 0 Create and fill a dt from the ADO recordset. DataTable dt new DataTable Orders new OleDbDataAdapter .Fill dt rs Bind the default view of the dt to the grid. Discussion One of the overloads of the OLE DB .NET 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