Results 1 to 5 of 5

Thread: [RESOLVED] [2005] General question about selecting rows from a DataTable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Resolved [RESOLVED] [2005] General question about selecting rows from a DataTable

    I am trying to improve my understanding of accessing (selecting) rows from a DataTable. Below are three methods that I have tried and all seam to return expected results. There are no bound controls to the DataTable.

    My question is: Which method is the preferred method to select rows from a DataTable and why? Better efficiency, or more predictable, easier to code?

    Thanks to all who contribute to this great forum.

    Code:
    EXAMPLE 1
    
    str_SQL = "GameID Like " & "'" & str_CurentGameID & "'"
    Dim rows As DataRow() = Me.ScoreboardDataSet.tbl_MatchData.Select(str_SQL)
    For Each row As DataRow In rows
          row("GameID") = str_NewGameID
    Next
    Code:
    EXAMPLE 2
                    
    str_SQL = "GameID Like " & "'" & str_CurentGameID & "'"
    Me.ScoreboardDataSet.tbl_MatchData.DefaultView.RowFilter = str_SQL
    Dim row As DataRow
    For Each view As DataRowView In Me.ScoreboardDataSet.tbl_MatchData.DefaultView
    row = DirectCast(View.Row, DataRow)
          row("GameID") = str_NewGameID
    Next view
    Code:
    EXAMPLE 3
    
    For Each row As ScoreboardDataSet.tbl_MatchDataRow In Me.ScoreboardDataSet.tbl_MatchData.Select(String.Format("GameID LIKE '{0}'", str_CurentGameID))
          row.GameID = str_NewGameID
    Next Mrow

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] General question about selecting rows from a DataTable

    I would generally only use the DefaultView.RowFilter in cases where I was binding, so example 2 is out.

    Examples 1 and 3 are basically exactly the same except that example 1 is drawn out. Some would consider example 1 better for the sake of clarity, but I'd only go along with that for rank beginners. For anyone who has any reasonable understanding of VB example 3 is the way to go. It's concise: brief and clear.

    Just note that you have lost some of your type safety in example 1. There's no need to declare the loop counter as type DataRow. It can be declared as type ScoreboardDataSet.tbl_MatchDataRow just like it is in example 3. You're enumerating exactly the same array in each case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Re: [2005] General question about selecting rows from a DataTable

    Just note that you have lost some of your type safety in example 1...
    Is the “Select(String.Format("GameID LIKE '{0}'", str_CurentGameID))” in Example 3 part of the added type safety” (over Example 1) or is the lost type safety in Example 1 only caused by declaring loop counter as type DataRow?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] General question about selecting rows from a DataTable

    I'd always do this:
    vb.net Code:
    1. String.Format("GameID LIKE '{0}'", str_CurentGameID)
    rather than this:
    vb.net Code:
    1. "GameID LIKE '" & str_CurentGameID & "'"
    but that's not what I meant. I was actually talking about this:
    vb.net Code:
    1. row("GameID") = str_NewGameID
    in place of this:
    vb.net Code:
    1. row.GameID = str_NewGameID
    It's not a big deal but you created a typed DataSet for a reason so you may as well use it. By declaring the loop counter as your custom DataRow type you are able to use the second option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Thumbs up Re: [2005] General question about selecting rows from a DataTable

    Thank you... These are rules I can use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width