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

Tài liệu Combining Data in Tables from Heterogeneous Data Sources docx

2 304 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 2
Dung lượng 12,87 KB

Nội dung

[ 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 " + "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 ] . 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. is shown in Example 3-6 . Example 3-6. File: CombiningDataFromMultipleDatabasesForm.cs // Namespaces, variables, and constants using System; using System.Configuration;

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