intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Combining Data in Tables from Heterogeneous Data Sources

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:2

95
lượt xem
7
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

[ Team LiB ] Recipe 3.6 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.

Chủ đề:
Lưu

Nội dung Text: Combining Data in Tables from Heterogeneous Data Sources

  1. [ Team LiB ] Recipe 3.6 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 " +
  2. "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. [ Team LiB ]
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2