Lecture-05 Turning Data Into Information ADO.net

download Lecture-05 Turning Data Into Information ADO.net

of 13

Transcript of Lecture-05 Turning Data Into Information ADO.net

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    1/13

    Modern Programming Language

    Lecture-05

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    2/13

    Turning Data into Information

    AgendaReturn a value that aggregates data from a tablecolumn

    Add a column that aggregates data from a table, or from its parent or child table

    Build an index-based view of a table

    Generate a new table based on a projected view of the original table

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    3/13

    Aggregating Data

    An aggregation function returns a single calculatedvalue from a set of related values.

    ADO.NET includes seven aggregation functionsfor use in expression columns and other DataTablefeatures.Sum: Calculates the total of a set of columnvalues. The column being summed must benumeric, either integral or decimal.

    Avg : Returns the average for a set of numbers ina column. This function also requires a numericcolumn.

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    4/13

    Cont Min: Indicates the minimum value found within a set of column values. Numbers, strings, dates, and other types of data that can be placed in order are all valid for the targetcolumn.

    Max: Like Min, but returns the largest value from theavailable column values. As with the Min function, mostcolumn types will work.

    Count: Simply counts the number of rows included in theaggregation. You can pass any type of column to thisfunction. As long as a row includes a non-NULL value in thatcolumn, it will be counted as 1.

    StDev: Determines the statistical standard deviation for aset of values, a common measure of variability within such aset. The indicated column must be numeric.

    Var: Calculates the statistical variance for a set of numbers,another measurement re-lated to the standard deviation.Only numeric columns are supported.

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    5/13

    Dim employees As New DataTable("Employee")

    employees.Columns.Add("ID", GetType(Integer))employees.Columns.Add("Gender", GetType(string))

    employees.Columns.Add("FullName", GetType(String))

    employees.Columns.Add("Salary", GetType(Decimal))

    ' ----- Add employee data to table, then...Dim averageSalary As Decimal = CDec(employees.Compute("Avg(Salary)", ""))

    The Compute method calculates the average of the values in the Salary column.The second argument to Compute is a flter that limits the rows included in thecalculation.

    Dim femalesInCompany As Integer = CInt(employees.Compute("Count(ID)", "Gender = 'F'"))

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    6/13

    Adding an Aggregate Column

    Dim sports As New DataTable("Sports")sports.Columns.Add("SportName", GetType(String))

    sports.Columns.Add("TeamPlayers", GetType(Decimal))

    sports.Columns.Add("AveragePlayers", GetType(Decimal), "Avg(TeamPlayers)")

    sports.Rows.Add({"Baseball", 9})sports.Rows.Add({"Basketball", 5})

    sports.Rows.Add({"Cricket", 11})

    MessageBox.Show(CStr(sports.Rows(0)!AveragePlayers)) ' Displays 8.3...

    MessageBox.Show(CStr(sports.Rows(1)!AveragePlayers)) ' Also 8.3...

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    7/13

    Aggregating Data Across Related Tables' ----- Build the parent table and add some data.

    Dim customers As New DataTable("Customer")

    customers.Columns.Add("ID", GetType(Integer))

    customers.Columns.Add("Name", GetType(String))

    customers.Rows.Add({1, Ali"})

    customers.Rows.Add({2, Khan"})

    ' ----- Build the child table and add some data. The "Total"

    ' expression column adds sales tax to the subtotal.

    Dim orders As New DataTable("Order")

    orders.Columns.Add("ID", GetType(Integer))

    orders.Columns.Add("CustomerID", GetType(Integer))

    orders.Columns.Add("Subtotal", GetType(Decimal))

    orders.Columns.Add("TaxRate", GetType(Decimal))orders.Columns.Add("Total", GetType(Decimal), "Subtotal * + TaxRate")

    ' ----- Two sample orders for customer 1, 1 for customer 2.

    orders.Rows.Add({1, 1, 35.24, 0.0875}) ' Total = $38.32

    orders.Rows.Add({2, 1, 56.21, 0.0875}) ' Total = $61.13

    orders.Rows.Add({3, 2, 14.94, 0.0925}) ' Total = $16.32

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    8/13

    Cont ' ----- Link the tables within a DataSet.

    Dim business As New DataSet

    business.Tables.Add(customers)

    business.Tables.Add(orders)

    business.Relations.Add(customers.Columns!ID, orders.Columns!CustomerID)

    ' ----- Here is the aggregate expression column.customers.Columns.Add("OrderTotals", GetType(Decimal), "Sum(Child.Total)")

    ' ----- Display each customer's order total.

    For Each scanCustomer As DataRow In customers.RowsTextBox1.Text &= (CStr(scanCustomer!Name) & ": " & scanCustomer!OrderTotals & vbCrLf)

    Next scanCustomer

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    9/13

    Setting Up Indexed Views

    The DataTable.Select method lets you apply aselection query to a table, returning a subset of theavailable rows in the DataTable.

    Its convenient, but if you will run the same queryagainst the table repeatedly, its not the mostefficient use of computing resources.

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    10/13

    DataView class ADO.NET includes the DataView class. As with the

    DataTable class, each DataView exposes a set of DataRow objects. But unlike the DataTable, theDataView does not actually contain any DataRowinstances. Instead, it contains an index that refers torows in a true DataTable.

    It builds this index using the same query expressionsused by the DataTable.Select method, with support for both a row selection component and a sortingcomponent.

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    11/13

    Creating a DataViewTo create a DataView from a DataTable, pass the table

    to the DataView constructor.Dim someView As New DataView(someTable)

    The new view includes all the rows in the original table,sorted in the order they appear in the DataTable.

    To alter the included rows, set the DataView objectsRowFilter property.

    Dim managersOnly As New DataView(employees)

    managersOnly.RowFilter = "IsManager = True"

    To sort the views rows, set the DataView.Sort property,

    managersOnly.Sort = "HireDate DESC"

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    12/13

    Using a DataView

    The DataView class includes several features thatreturn information about the in-view rows. Themost basic is the DataView.Count property

    Dim managersOnly As New DataView(employees)

    managersOnly.RowFilter = "IsManager = True"MessageBox.Show("Total Managers: " & managersOnly.Count)

  • 7/28/2019 Lecture-05 Turning Data Into Information ADO.net

    13/13

    Questions???