Combining Data in Tables from Heterogeneous Data Sources

Problem

You want to create a report that is based on data from tables in more than one data source.

Solution

Use ad-hoc connector names in SQL statements.

The sample code retrieves data from both a SQL Server table and a Microsoft Access table to create a single result set. Specifically, Northwind Order data is retrieved from SQL Server and Northwind Order Details data is retrieved from Access and joined to the Order information.

The C# code is shown in Example 3-6.

Example 3-6. File: CombiningDataFromMultipleDatabasesForm.cs

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

// . . . 

// Fill the table with data from SQL Server and MS Access.
String sqlSelect = "SELECT o.OrderID, o.CustomerID, o.OrderDate, " +
 "od.ProductId, od.UnitPrice, od.Quantity, od.Discount " + 
 "FROM Orders o INNER JOIN " + 
 "OPENROWSET('Microsoft.Jet.OLEDB.4.0','" +
 ConfigurationSettings.AppSettings["MsAccess_Database_Filename"] +
 "';'admin';'',[Order Details]) " +
 "AS od ON o.OrderID = od.OrderID " +
 "ORDER BY o.OrderID, od.ProductID";
SqlDataAdapter da = new SqlDataAdapter(sqlSelect,
 ConfigurationSettings.AppSettings["Sql_ConnectString"]);
DataTable dt = new DataTable( );
da.Fill(dt);

// Set up and bind a view with data from both tables.
DataView dv = dt.DefaultView;
dv.AllowDelete = false;
dv.AllowEdit = false;
dv.AllowNew = false;
dataGrid.DataSource = dv;

Discussion

Microsoft SQL Server 2000 and later supports two methods to access data from heterogeneous data sources through OLE DB: linked servers and ad hoc connector names.

You can refer to linked servers in SQL statements using a four-part name comprised of the names of the linked server, the catalog, the schema within the catalog, and data object. These names are separated with periods. If the data sources are going to be accessed frequently, defining them as linked servers rather than through ad hoc connector names as shown in the sample will improve performance. For more information about using linked servers, see Microsoft SQL Server Books Online.

Ad-hoc connector names allow data from heterogeneous data sources to be accessed without setting up linked servers by providing the information required to connect to each data source in the SQL statement. This is done using either the OPENROWSET or the OPENDATASOURCE function to open the row set from the OLE DB data source. Both functions take arguments containing all connection information required to access the data source. The functions allow the row sets to be subsequently referenced like any other table in SQL statements.

For more information about OPENROWSET and OPENDATASOURCE functions, see Microsoft SQL Server Books Online.

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

show all menu



ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net