Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
[ 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. | 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.OrderlD o.CustomerlD o.OrderDate od.ProductId od.UnitPrice od.Quantity od.Discount FROM Orders o INNER JOIN OPENROWSET Microsoft.Jet.OLEDB.4.O ConfigurationSettings.AppSettings MsAccess_Database_Filename admin Order Details AS od ON o.OrderlD od.OrderlD ORDER BY o.OrderlD 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