Results 1 to 9 of 9

Thread: update table periodically

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    57

    update table periodically

    I am working on some code in vb.net that will run periodically to update a sql2005 database table.

    table structure is very simple.

    tblserver
    Servername (Keyfield)
    item1
    item2
    etc.

    anyway. I need to run an update to the database and insert records if the server does not exists in the table
    I can do a loop
    Code:
    for each input server 
    if input server = tbleserver.servername
    tblserver.servername.next
    you get the point.
    that seems pretty inefficent, given that I would have to check each of the 400 + servers against the current database

    Any recommendations?

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

    Re: update table periodically

    You need to use an If statement somewhere because you need to know If the server already exists. You can do it in your VB code or your SQL code. If you want to do it in VB then you can populate a DataTable with all the records, then for each server you want to ensure is in the table you do this:
    vb.net Code:
    1. If myDataTable.Select(String.Format("Servername = '{0}'", serverName)).Length = 0 Then
    2.     'There are no matching rows so add one.
    3. End If
    and then at the end save the whole DataTable. If you did it in SQL the principle would be exactly the same.
    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
    Member
    Join Date
    Jul 2007
    Posts
    57

    [RESOLVED] Re: update table periodically

    Thanks for your assistance.
    Last edited by jconway; Jul 24th, 2008 at 08:08 AM. Reason: resolved

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: update table periodically

    Where are you getting the names of the 400 servers?

    Usually I would suggest loading all the "potential new servers" into a staging table or temp table. Fastest way to do this is with some kind of BULK INSERT (from a file or using methods that do this from memory).

    Then you simply say:

    Code:
    Insert into tblServer
        Select WT.* From WorkTable WT
        Left Join tblServer TS on TS.ServerName=WT.ServerName
        Where TS.ServerName is null
    The WHERE clause determines the fact of the servername not being in tblServer.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    57

    Re: update table periodically

    I am getting the list from mfcom we have a very large citrix farm and I am working on some managment reports. Enumeration at runtime while an options has a large cost, and server names do not change that often.

    So I load to a sql table and this process runs 2x a month automatically.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: update table periodically

    Then - as I said - if you can get the servernames into a staging table - bulk insert will load them relatively instantly (hundreds of thousands of rows in seconds usually)...

    Then that INSERT/SELECT I showed will run in less then seconds.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    57

    Re: update table periodically

    Quote Originally Posted by szlamany
    Then - as I said - if you can get the servernames into a staging table - bulk insert will load them relatively instantly (hundreds of thousands of rows in seconds usually)...

    Then that INSERT/SELECT I showed will run in less then seconds.
    I think I understand the concepts now. thanks for helping with that.

    Now, being new to the .net environment, I am using the Dataset wizard to create the base code for me, which is pretty cool.

    How can I modify the insert code in the query builder to deal with checking to see if the data exists?
    here is what I have so far.

    Code:
    Public Sub Loadservers()
            Dim Worktable As New DataTable("wt")
            Worktable.Columns.Add("servername", GetType(System.String))
            Worktable.Columns.Add("Load_eval", GetType(System.String))
            Worktable.Columns.Add("Zone", GetType(System.String))
            Worktable.Columns.Add("dateadded", GetType(Date))
            Dim servertableadapter As New serverupdatedsTableAdapters.serverTableAdapter
            Dim serverlist As serverupdateds.serverDataTable
            serverlist = servertableadapter.GetData
            thefarm = New MetaFrameFarm
            thefarm.Initialize(MetaFrameObjectType.MetaFrameWinFarmObject)
            Dim aserver As New MetaFrameServer
            Dim theloadobject As New MetaFrameLoadEvaluator
            Dim worktablerow As DataRow
            For Each aserver In thefarm.Servers
                worktablerow = Worktable.NewRow
                Dim theservername As String = aserver.ServerName
                Dim theloadeval As String
                theloadobject = aserver.AttachedLE
                theloadobject.LoadData(1)
                theloadeval = theloadobject.LEName.ToString
                Dim thezone As String = aserver.ZoneName
                Dim thetimeadded As Date = Date.Now()
                worktablerow(0) = theservername
                worktablerow(1) = theloadeval
                worktablerow(2) = thezone
                worktablerow(3) = thetimeadded
                Worktable.Rows.Add(worktablerow)
    'I know I need to do something here.  but what?>>>>
                'servertableadapter.Insert(theservername, theloadeval, thezone, thetimeadded)
    
            Next
    
        End Sub
    Last edited by jconway; Jul 25th, 2008 at 11:56 AM.

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    57

    Re: update table periodically

    ok, I have more information. maybe I just don't get the datatable stuff yet.

    here is my updated code

    Code:
    Public Sub Loadservers()
            Dim Worktable As New DataTable("wt")
            Worktable.Columns.Add("servername", GetType(System.String))
            Worktable.Columns.Add("Load_eval", GetType(System.String))
            Worktable.Columns.Add("Zone", GetType(System.String))
            Worktable.Columns.Add("dateadded", GetType(Date))
            Dim servertableadapter As New serverupdatedsTableAdapters.serverTableAdapter
            Dim serverlist As serverupdateds.serverDataTable
            serverlist = servertableadapter.GetserverData
            thefarm = New MetaFrameFarm
            thefarm.Initialize(MetaFrameObjectType.MetaFrameWinFarmObject)
            Dim aserver As New MetaFrameServer
            Dim theloadobject As New MetaFrameLoadEvaluator
            Dim worktablerow As DataRow
            For Each aserver In thefarm.Servers
                worktablerow = Worktable.NewRow
                Dim theservername As String = aserver.ServerName
                Dim theloadeval As String
                theloadobject = aserver.AttachedLE
                theloadobject.LoadData(1)
                theloadeval = theloadobject.LEName.ToString
                Dim thezone As String = aserver.ZoneName
                Dim thetimeadded As Date = Date.Now()
                worktablerow(0) = theservername
                worktablerow(1) = theloadeval
                worktablerow(2) = thezone
                worktablerow(3) = thetimeadded
                Worktable.Rows.Add(worktablerow)
            Next
            servertableadapter.Insertserverswithoutdupblicates()
    
        End Sub
    Insertserverswithoutdupblicates sql query is this
    Code:
    INSERT INTO server
                          (ServerName, Load_eval, Zone, dateadded)
    SELECT     Worktable.ServerName, Worktable.Load_eval, Worktable.Zone, Worktable.dateadded
    FROM         server AS server_1 LEFT OUTER JOIN
                          Worktable ON server_1.ServerName = Worktable.ServerName
    WHERE     (server_1.Servername IS NULL)
    I get the following exception.

    System.Data.SqlClient.SqlException was unhandled
    Class=16
    ErrorCode=-2146232060
    LineNumber=1
    Message="Invalid object name 'Worktable'."

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    57

    Re: update table periodically

    Solved my own problem, I was making it way to hard.

    I was able to accomplish this with the datatable.clone and merge methods.

    Code:
     Public Sub Loadservers()
            Dim servertableadapter As New serverupdatedsTableAdapters.serverTableAdapter
            Dim serverlist As serverupdateds.serverDataTable
            serverlist = servertableadapter.GetserverData
            thefarm = New MetaFrameFarm
            thefarm.Initialize(MetaFrameObjectType.MetaFrameWinFarmObject)
            Dim aserver As New MetaFrameServer
            Dim theloadobject As New MetaFrameLoadEvaluator
            Dim Worktable As New DataTable()
            Worktable = serverlist.Clone
            Dim worktablerow As DataRow
            For Each aserver In thefarm.Servers
                worktablerow = Worktable.NewRow
                Dim theservername As String = aserver.ServerName
                Dim theloadeval As String
                theloadobject = aserver.AttachedLE
                theloadobject.LoadData(1)
                theloadeval = theloadobject.LEName.ToString
                Dim thezone As String = aserver.ZoneName
                Dim thetimeadded As Date = Date.Now()
                worktablerow(0) = theservername
                worktablerow(1) = theloadeval
                worktablerow(2) = thezone
                worktablerow(3) = thetimeadded
                Worktable.Rows.Add(worktablerow)
            Next
            serverlist.Merge(Worktable, False, MissingSchemaAction.Error)
            servertableadapter.Update(serverlist)
    End Sub

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