tailieunhanh - Executing Queries That Use COMPUTE BY

[ Team LiB ] Recipe Executing Queries That Use COMPUTE BY Problem The SQL Server .NET data provider does not support the COMPUTE BY clause but you want to execute a COMPUTE BY statement using . | Team LiB Recipe Executing Queries That Use COMPUTE BY Problem The SQL Server .NET data provider does not support the COMPUTE BY clause but you want to execute a COMPUTE BY statement using . Solution Use the COMPUTE BY statement from the Command object of the OLE DB .NET data provider. The sample code defines a COMPUTE BY statement and executes it using the ExecuteReader method of the OleDbCommand object. Multiple result sets are returned by the DataReader and then these are displayed. The C code is shown in Example 3-13. Example 3-13. File Namespaces variables and constants using System using using using . . . StringBuilder result new StringBuilder String sqlSelect select OrderlD ProductID Quantity FROM Order Details ORDER BY ProductID COMPUTE SUM quantity by ProductID OleDbConnection conn new OleDbConnection OleDb_Shape_ConnectString OleDbCommand cmd new OleDbCommand sqlSelect conn OleDbDataReader dr do Order tProduct tQuantity while 0 t 1 t 2 Get the sum. SUM t t 0 while Discussion The SQL Server .NET data provider does not support the COMPUTE BY clause but the OLE DB .NET data provider does. The results are returned as multiple pairs of result sets the first of which contains the selected details and the second containing the results of the aggregate functions specified the sum of the quantity ordered for the product in this example in the COMPUTE BY clause. This pattern is repeated for the remaining pairs of result sets. Microsoft states that the COMPUTE and COMPUTE BY clauses are provided in SQL Server and later versions for backward compatibility.