Results 1 to 7 of 7

Thread: [RESOLVED] items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Resolved [RESOLVED] items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

    I'm using VS 2005, i'll be moving info from a Dictionay to Sql Server table, i can do it row by row (1 insert command by item), but i have almost 1000 files loaded with 300.000+ records and i'm implementing different way to make this faster (table indexes, intermediate tables, etc..). One thing that would make all this faster would be a direct copy from dict/list/array to sql server table, is it possible? i could join/concat all these 1000 records (3 fields each) and send to db something like:

    Insert...
    Select ...
    Union all
    Select
    Union All
    Select..

    but as you can see this this doesn't look very nice, i doubt there is another way but just in case, if anybody knows, it would be nice.

    The reason i need C# between the files and the table is because much data processing is done in C#, data is not inserted into db just as it comes from files.

    Good old DTS packages could do the job but i would have to replace this C# App by the DTS pack, and also im using Sql Server 2008 (DTS's have been deprecated, i don't understand why)

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

    Re: items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

    Why isn't it in a Datatable? is there a reason it's in a List/Dictionary?

    -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??? *

  3. #3

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

    I can use anything, my last idea was adding this post-processed data in a Dictionary using chunks of 1000 rows then trying to do something like a Bulk insert then reading another 1000 and so on, i can use any container, but is there any way to do something like a bulk insert so i don't have to insert 1 record each time? I've been reading some examples about SqlBulkInsert but all these examples take data from a CSV file, maybe there is a way to do the same using some container in memory, like array/list/dictionary or a datatable.

    I've come to a working version of whta i wanted, but it's ugly:

    - I add 100 1000 records( taken from file and post processed) to a Dictionary ensuring they are all unique records (this is part of this post-processing)
    - I use a StringBuilder to concatenate all queries like:

    Insert into temp_table
    select field1, field2, field3
    union all
    select field1, field2, field3
    union all .. and so on 1000 times.

    - After that in a stored procedure i join these temp table with the real table to the the records not present in the real table and just insert those records
    - Finally i erase the temp table, erase the stringbuilder, and proceed to the next loop.

    It's working fast now, but it's ugly, it's inserting a giant sql string, and also i dont like to create Sql Strings by concatenating them with values, it's not a good practice, i usually use SqlParameters when i can, but i can't use them when doing this ( i would need like 3K params i think)

    So, if something like a Bulk-Insert can be made directly from data in memory, it would be the best.

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

    Re: items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

    You really only have two choices.

    Run each insert one at a time - doesn't matter if it's a datatable or whatever - it's still taking t-sql dml statements in some fashion. There isn't some magic way to make them bulk-transmit...

    or...

    Run bulk insert.

    300,000 rows is a point where bulk insert is the heavy favorite. If you had a million rows you would only ever consider bulk insert - right?

    Bulk insert is different and better for large jobs because it runs on the server with said server opening a text file for the import of data. Usually bypassing logging in some way to actually make the speed-enhancement even better.

    *** 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
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

    I would read the data into a datatable, mainuplate it as needed then feed that (as a datatable) to the =http://social.msdn.microsoft.com/Search/en-US?query=sqlbilkcopy&ac=3]SQLBulkCopy[/url] class ... set the DT as the source, give the table name as the desitination, set the mappings, open the connection, and suck away.

    -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??? *

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

    Re: items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

    Read a bit of this

    http://msdn.microsoft.com/en-us/libr...ql.100%29.aspx

    75% of the way through the read you get to Optimizing Bulk Load...

    You have to remember that bulk load runs on the server - whether from a text file (old school) or some memory stream - it still runs locally and you have other issues / constraints (such as keeping up) that matter.

    Are you going to have 300,000+ rows daily? Are you trying to automate such a process?

    *** 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
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: items in Dictionary(Of TKey, TValue) or List, To Sql Server table in one call?

    Thanks i'll follow those links.

    This only runs on demand, the info contained in these files is the typical report provided by IIS about different websites, this is sort of report is typically used by webmaster tools like cPanel to show all info/statistics from websites, the only info i take from it is IP address/cell phone number/carrier, i use substring to get this info from each line but it's not at fixed positions so i first i search for known text at left/right of each value, this is the post-processing i was talking about.
    Last edited by jcis; Oct 19th, 2012 at 09:55 PM.

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