lecture-03 accessing the right data values ado.net

Upload: nazirbook

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    1/10

    Modern Programming Language

    Lecture-03

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    2/10

    Accessing the Right Data Values

    Agenda Find items in a DataTable by primary key

    Search for DataRow instances using standardquery statements

    Obtain a set of DataRow objects sorted by one ormore columns

    Add expression columns to a DataTable thatpresent calculated values

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    3/10

    Queryingand Sorting Data

    Although adding records to a DataTable is

    important, the real value of ADO.NET lies ingetting those records back out in a variety of ways.

    you can scan through every row in the table,checking each record as you encounter it to see

    whether it matches your search limits, theDataTable class already includes features that willlet you select just those DataRow instances thatmatch a selection rule.

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    4/10

    Finding Rows by Primary Key To locate a row based on its primary key, use the

    tables Rows.Find method. For tables with single-column keys, pass this method a key value of theappropriate data type.

    For multi-column keys, pass the key components as

    an array. Find returns a single DataRow instance forthe matching row.

    ' ----- Single-part key.

    Dim matchingRow As DataRow = someTable.Rows.Find(searchValue)

    ' ----- Multi-part key.

    Dim matchingRow As DataRow = someTable.Rows.Find({keyPart1,

    keyPart2, keyPart3})

    If no row matches the provided primary key, Find returns Nothing

    The method throws an exception if you apply it to tables with no

    defined primary key.

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    5/10

    Example

    Dim dr As DataRow = CustomerAccounts.Rows.Find(Me.TextBox1.Text)

    Dim ds As DataTable = CustomerAccounts.Clone()

    ds.ImportRow(dr)

    DGV.DataSource = ds

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    6/10

    Selecting Rows with a Search Criteria The Find method is useful when you need to retrieve a

    single row based on a primary key lookup value, butuseful data analysis typically involves searching acrossmany of a tables columns and returning all possiblematches. To provide this functionality, the DataTableclass includes the Select method.

    You pass the Select method a string that contains theselection criteria. When successful, the method returnsan array of matching DataRow instances from thetable.

    Dim matchingRows() As DataRow = someTable.Select(filterCriteria)

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    7/10

    Example

    Dim dr() As DataRow = dt.Select("ID

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    8/10

    Example-2 Search for String values

    Dim dr() As DataRow = dt.Select("Name LIKE '" & Me.TextBox1.Text.Trim & "%'")Dim dt2 As Data.DataTable = dt.Clone

    Dim r As DataRow

    For Each r In dr

    dt2.ImportRow(r)

    Next

    DataGridView1.DataSource = dt2

    Performing Case-Sensitive

    someTable.CaseSensitive = True

    Sort the The result:

    Dim dr() As DataRow = dt.Select("ID

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    9/10

    Using Expression Columns The DataTable class also supports expression columns,

    fields that expose a calculated result based on the data in

    other row columns.

    For instance, if your table of orders includes a Subtotalcolumn and a Tax column, you could add an expressioncolumn named Total that calculated the sum of Subtotal

    and Tax. ' ----- Syntax using a DataColumn object.

    Dim orderTotal As New DataColumn

    orderTotal.ColumnName = "Total"

    orderTotal.DataType = GetType(Decimal)

    orderTotal.Expression = Balance1+ ISNULL(Balance2, 0)"

    someTable.Columns.Add(orderTotal)

    ' ----- Syntax using Add arguments only.

    someTable.Columns.Add("Total", GetType(Decimal), Balance1+ ISNULL(Balance2, 0)" )

  • 7/28/2019 Lecture-03 Accessing the Right Data Values ADO.net

    10/10

    Questions???