PC Review


Reply
Thread Tools Rate Thread

best practice for loading large files into SQL Server database

 
 
Phil Johnson
Guest
Posts: n/a
 
      23rd Nov 2007
Hello,

I am working on a peice of code that takes a file and loads it into a SQL
Server DB.

When I load large files (over 70Mb) I get a system out of memory exception.
My machine has 2G RAM

I think the problem is that a file stream pointing to the file on the file
system is loaded into a memory stream one byte at a time. Does anybody have
any pointers on doing this type of thing and how to do this so that the file
is actually streamed from the file system right into the SQL Server DB?

Any advice or links to articles on how to do this best etc would be really
appreciated.

Thanks in advance for any help

--
Regards,

Phil Johnson (MCAD)
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Nov 2007
Well, what is the file and what code are you using to look at it?

To insert lots of rows (i.e. a huge CSV) you should be using
SqlBulkCopy with a streaming IDataReader; I recommend the one here:
http://www.codeproject.com/cs/database/CsvReader.asp

To insert a single huge BLOB, you must use chunking techniques - i.e.
you need to allocate a buffer (8040 bytes is optimal for SQL Server, I
believe) and read buffer-fulls of info, and pass this down to the db
in chunks, using the appropriate BLOB SQL commands [the commands
change between SQL 2000 with "image", and SQL 2005 with
"varbinary(max)"].

If you try and load the entire BLOB into memory (as a huge byte[]),
yes it will fail.

If you try and load the entire file into memory (as a DOM, DataTable,
etc), yes it will fail.

Marc


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Nov 2007
I found some of my old BLOB examples...

stream=>database
http://groups.google.co.uk/group/mic...4e7e3782e59a93

database=>stream
http://groups.google.co.uk/group/mic...d173f1db2951f1

Marc


 
Reply With Quote
 
Phil Johnson
Guest
Posts: n/a
 
      23rd Nov 2007
Thanks Marc,

It is indeed a BLOB that I am loading, not a csv.

Those links look ideal.

Thanks again.
--
Regards,

Phil Johnson (MCAD)


"Marc Gravell" wrote:

> I found some of my old BLOB examples...
>
> stream=>database
> http://groups.google.co.uk/group/mic...4e7e3782e59a93
>
> database=>stream
> http://groups.google.co.uk/group/mic...d173f1db2951f1
>
> Marc
>
>
>

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Nov 2007
No problem. If you need fewer round trips when saving, I believe that
any multiple of 8040 will work; likewise, if you are using SQL Server
2005 you should definitely use the varbinary(max) and associated
commands (instead of UPDATETEXT) - but the "read" stays the same.

For info, SQL Server 2008 has a new file-stream data type / attribute
(or something!). I haven't played with it yet, but that may also be
useful for file-based scenarios. YMMV.

Marc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best practice for streaming fairly large files from server to clie Phil Johnson Microsoft ASP .NET 9 28th Nov 2007 03:21 PM
Moving (or Archive ) records to another database (access / Sql Server). best practice ? nms@bioenabletech.com Microsoft ADO .NET 2 11th Dec 2006 08:26 AM
AutoFilter Best Practice when used in large files (slow system dow =?Utf-8?B?RGVubmlz?= Microsoft Excel Misc 2 17th Feb 2006 07:53 PM
Best practice Question on Compaq Server and 2003 loading SP1 Adam Raff Microsoft Windows 2000 6 2nd Nov 2005 03:34 PM
Loading large files Gregor Wind Microsoft Dot NET 1 16th Jun 2004 03:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:40 AM.