Results 1 to 11 of 11

Thread: [2005] Speed Question - DataTables

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    [2005] Speed Question - DataTables

    Hi Guys!

    I realize this is a question I could answer myself with some testing but wanted your opinions.

    I am looking to see if a certain row exists in a dataset (2000+rows). I have two methods.

    1) Brute Force
    VB Code:
    1. For iRow = 0 To objTblMaillist.Rows.Count - 1 Step 1
    2.    If Not (objTblMaillist.Rows(iRow).Item(0) Is GetType(System.DBNull)) Then
    3.       If CStr(objTblMaillist.Rows(iRow).Item(0)) = sShortCode Then
    4.          bFound = True
    5.          Exit For
    6.       End If
    7.    End If
    8. Next iRow

    vs

    2) Framework

    VB Code:
    1. objTblMaillist.PrimaryKey = New System.Data.DataColumn() {objTblMaillist.Columns(0)}
    2. objTblMaillist.DefaultView.Sort = "Source"
    3.  
    4. If objTblMaillist.DefaultView.Find(sShortCode) >= 0 Then
    5.    bFound = True
    6. End If

    So what do think would be quicker?

    Thanks,
    Matt

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

    Re: [2005] Speed Question - DataTables

    I assume that you're not attempting to find this record by its primary key. If you were then you could simply call the Find method of the table's Rows collection. I very much doubt that the first method would be quicker than the second but I couldn't say for sure. You could also do this:
    VB Code:
    1. bFound = objTblMaillist.Select("Source = " & sShortCode).Length > 0
    Also, I have to comment on your naming convention as I'm such a big fan (NOT) of Hungarian notation. The point of Hungarian notation is to be able to easily ascertain the type of a variable by it's prefix. What's going on with "objTblMaillist". The prefix "obj" is in almost all cases completely pointless. It may have told you something back in the days when there weren't many types in existence but these days there are so many types that using "obj" is beyond pointless because it tells you nothing about the actual type of the variable. The ONLY time it could possibly be considered a good thing is if the variable is actually type Object. What's more, you've even got a second prefix in there anyway. You're presumably including the "Tbl" part to indicate that it's a table so what is "obj" for? If your variable is type DataTable then the only possible logical choices for names would be "tblMailList" or "dtMailList".
    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
    Mar 2006
    Posts
    65

    Re: [2005] Speed Question - DataTables

    Hey Jm,

    Yeah I understand and agree with what your saying about the naming convention. I do like Hungarian notation but agree its gone overboard here. The code has been ammended.

    No I am not trying to find a row by its primary key, the row actually only contains one the column. I am simply interested to see if row exists nothing more.

    Thanks JM,
    Matt

    P.S Cant give you anymore Rep, it wants me to 'spread it round' a little....Will do and get back to ya...

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Speed Question - DataTables

    How is your datatable being filled? If the datatable only contains one column you could use a collection object, and the indexOf method.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] Speed Question - DataTables

    Hey Bill!

    I am filling the datatable using a SQL client dataadapter...

    Matt.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Speed Question - DataTables

    Are you filling your datatable just so you can check for this? If so, you would be much better off doing a query to check for the row you want, returning one value and using the .ExecuteScalar instead.....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] Speed Question - DataTables

    I sure could tech but its a little more serious than that.

    My goal for this function is to return a datatable that meets a certain criteria. This datatable contains is list of unique id's (sShortCode). Firstly I pull a list id's from one server then I get a SQLCmd Reader from another database server and begin a filtering process.

    Give me a sec and I'll post the whole code (*brace myself for some constructive criticism*)....

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] Speed Question - DataTables

    Well here we go....

    VB Code:
    1. Private Shared Function GetExecCampaignList() As Data.DataTable
    2.         'This function returns a datatable of campaign shortcodes that meet criteria:
    3.         '1. Campaign is less than six months old, or has been distributed in the last six months- iExecLookBack (constant), local variable: dtCompareDate
    4.         '2. Have produced leads at any time
    5.  
    6.         Dim objSQLCmd As Data.SqlClient.SqlCommand
    7.         Dim objSQLDR As Data.SqlClient.SqlDataReader
    8.         Dim objSQLDA As Data.SqlClient.SqlDataAdapter
    9.  
    10.         Dim dtMaillist As Data.DataTable                        'Maillist Table
    11.         Dim dtCampaigns As Data.DataTable                       'Result DataTable
    12.         Dim drRow As Data.DataRow
    13.  
    14.         Dim sSQL As String
    15.         Dim dtCompareDate As DateTime
    16.         Dim sShortCode As String                                'Campaign Shortcode
    17.         Dim dtCampDate As DateTime                              'Campaign Creation Date
    18.         Dim sMedia As String                                    'Camapign Media
    19.         Dim bLead As Boolean                                    'Campaign has produced a lead
    20.         Dim bFresh As Boolean                                   'Campaign created or distributed within last 6 months
    21.  
    22.         Try
    23.             dtCompareDate = DateTime.Now.AddMonths(-iExecLookBack)
    24.  
    25.             'Define a result datatable
    26.             dtCampaigns = New Data.DataTable
    27.             dtCampaigns.Columns.Add("ShortCode", GetType(String))
    28.             dtCampaigns.Columns.Add("Media", GetType(String))
    29.             dtCampaigns.Columns.Add("Desc", GetType(String))
    30.  
    31.             'Pull the mailist sources from go live date on
    32.             sSQL = "SELECT DISTINCT [Source] FROM Maillist " & _
    33.                    "WHERE ([DateStamp] >= '" & cMain.conGoLiveDate.ToString(cMain.conRptDate) & "') " & _
    34.                    "AND ([SOURCE] IS NOT NULL)"
    35.  
    36.             objSQLDA = New SqlClient.SqlDataAdapter(sSQL, cMain.gsLdrConn)
    37.  
    38.             dtMaillist = New Data.DataTable
    39.             objSQLDA.Fill(dtMaillist)
    40.  
    41.             dtMaillist.PrimaryKey = New System.Data.DataColumn() {dtMaillist.Columns(0)} 'Set the primary key to allow DefaultView.Find()
    42.             dtMaillist.DefaultView.Sort = "Source"
    43.  
    44.             'Pull a list of all campaigns (Normal and Internet)
    45.             sSQL = "SELECT [Index],[ShortCode],[Media],[Desc],[Date] FROM tblCampaigns " & _
    46.                    "UNION ALL " & _
    47.                    "SELECT [Index],[ShortCode],'Internet' as [Media],[Desc],[StartDate] FROM tblInetCampaigns"
    48.  
    49.             objSQLCmd = New SqlClient.SqlCommand(sSQL, New SqlClient.SqlConnection(cMain.gsMktConn))
    50.             objSQLCmd.Connection.Open()
    51.  
    52.             objSQLDR = objSQLCmd.ExecuteReader(CommandBehavior.CloseConnection)
    53.  
    54.             With objSQLDR
    55.                 While .Read()
    56.                     'Check all campaigns to see if they meet the selection criteria
    57.  
    58.                     sShortCode = .GetString(1)
    59.                     sMedia = .GetString(2)
    60.                     dtCampDate = .GetDateTime(4)
    61.  
    62.                     bLead = False
    63.                     bFresh = False
    64.  
    65.                     'Test campaign to see if it meets criteria
    66.                     If dtMaillist.DefaultView.Find(sShortCode) >= 0 Then        'Check if the campaign shortcode is found in maillist results
    67.                         bLead = True
    68.                     Else
    69.                         Select Case sMedia
    70.                             Case "Mailouts", "Flyers"
    71.                                 sSQL = "SELECT COUNT(*) FROM tblDistQty WHERE ([CampaignIndex]=" & .GetInt32(0) & ")"
    72.                                 If CInt(cMain.GetDBVal(sSQL)) > 0 Then
    73.                                     bFresh = True
    74.                                 End If
    75.                             Case Else
    76.                                 If dtCampDate > dtCompareDate Then
    77.                                     bFresh = True
    78.                                 End If
    79.                         End Select
    80.                     End If                                                        'Check the campaign creation or last distribution
    81.  
    82.                     'If lead found or date criteria met -> add result
    83.                     If bLead Or bFresh Then
    84.                         drRow = dtCampaigns.NewRow()
    85.  
    86.                         drRow.Item(0) = sShortCode
    87.                         drRow.Item(1) = sMedia
    88.                         drRow.Item(2) = .GetString(3)
    89.  
    90.                         dtCampaigns.Rows.Add(drRow)
    91.                     End If
    92.  
    93.                 End While
    94.             End With
    95.  
    96.         Catch ex As Exception
    97.             cMain.WriteErrorLog(conCName & "::GetExecCampaignList" & vbTab & ex.Message)
    98.             Return dtCampaigns
    99.         Finally
    100.             drRow = Nothing
    101.  
    102.             dtMaillist.Dispose()
    103.             dtMaillist = Nothing
    104.  
    105.             If Not objSQLDA Is Nothing Then
    106.                 objSQLDA.Dispose()
    107.                 objSQLDA = Nothing
    108.             End If
    109.  
    110.             If Not objSQLCmd Is Nothing Then
    111.                 objSQLCmd.Dispose()
    112.                 objSQLCmd = Nothing
    113.             End If
    114.  
    115.             If Not objSQLDR Is Nothing Then
    116.                 If Not objSQLDR.IsClosed Then
    117.                     objSQLDR.Close()
    118.                 End If
    119.                 objSQLDR = Nothing
    120.             End If
    121.         End Try
    122.  
    123.         Return dtCampaigns
    124.  
    125.     End Function

    * JM - I haven't finished implementing the naming convention we spoke about...

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] Speed Question - DataTables

    As you can see the datatable and the datareader are using different connections.

    This could all be done using an SQL query but the problem is that the two separate DB's have different collation methods so I am left to do the filtering myself....

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

    Re: [2005] Speed Question - DataTables

    .NET 2.0 provides a completely new transactional model that allows you to enlist multiple connections in the same transaction and even enlist operations that don't involve database access, as long as they support transactions. The bonus is that it's easier to use than the old model too. Read up on the TransactionScope class.

    http://search.msdn.microsoft.com/sea...ansactionscope
    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] Speed Question - DataTables

    Thanks JM, will look further into it...

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