473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Read very large file in bytearray and upload to MSSQL

Hi,

I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL
2005.

My code looks like this :

fs = New FileStream(sFilePath, FileMode.Open)
Dim ByteArray(fs.Length) As Byte
fs.Read(ByteArray, 0, fs.Length)
fs.Close()

The problem is when I dim the bytearray with my 600Mb file, the
bytearray becomes invalid.
I think the bytearray can only accept an integer as dimension.

Since MSSQL's limit is 2Gb, how should I then write a big file to
MSSQL.
Is there another method to get the whole bytearray of a file to put
into MSSQL?

Regards,
Sven Peeters
Nov 18 '07 #1
11 4016
"Icemokka" <ic******@gmail.comschrieb
Hi,

I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL
2005.

My code looks like this :

fs = New FileStream(sFilePath, FileMode.Open)
Dim ByteArray(fs.Length) As Byte
fs.Read(ByteArray, 0, fs.Length)
fs.Close()
As this example shows, it is strongly recommended to switch Option Strict
On. By disabling it, you are not pointed to the fact that a file can be
larger than 2GB. Check the file size before, and if it's not >2GB, you can
safely convert the Long value returend by fs.Length to an Integer.
The problem is when I dim the bytearray with my 600Mb file, the
bytearray becomes invalid.
I think the bytearray can only accept an integer as dimension.
600MB is within the range of an integer, so this is not the problem.
Integer.MaxValue is 2GB.

Be aware that the dim/redim statements expect the upper bound of the array,
so replace "fs.length" by "Cint(fs.length) - 1"
Since MSSQL's limit is 2Gb, how should I then write a big file to
MSSQL.
Is there another method to get the whole bytearray of a file to put
into MSSQL?
Is your current problem getting the byte array into the database or reading
the file into the byte array? If it's the former, have a look at the ADO.Net
group because it is not a VB.Net language related problem. I'm also not sure
whether your problem is to store a 600+ MB file (up to 2GB) into the
database or if you also want to store 2GB+ there. Storing a link to the file
was the only solution (AFAIK).
Armin

Nov 18 '07 #2

Well i think it is hard to believe that you reach the uperlimit of
2,147,483,647. wich is the maximum size of an integer in .Net
wich in the case of a byte array is equivalant to 2047 + megabytes or 1.9
+ gigabytes
Michel

"Icemokka" <ic******@gmail.comschreef in bericht
news:9b**********************************@n20g2000 hsh.googlegroups.com...
Hi,

I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL
2005.

My code looks like this :

fs = New FileStream(sFilePath, FileMode.Open)
Dim ByteArray(fs.Length) As Byte
fs.Read(ByteArray, 0, fs.Length)
fs.Close()

The problem is when I dim the bytearray with my 600Mb file, the
bytearray becomes invalid.
I think the bytearray can only accept an integer as dimension.

Since MSSQL's limit is 2Gb, how should I then write a big file to
MSSQL.
Is there another method to get the whole bytearray of a file to put
into MSSQL?

Regards,
Sven Peeters

Nov 18 '07 #3
see this example

http://support.microsoft.com/kb/308042

if it doesn`t work, you are probably hitting another limit ( system
resources , timeout ?? )

Another solution would be to save your file on a file server , and only
store the key and path to the file in the database
mostly SQL dba`s don`t like these hughe blob fields in there database .

HTH

Michel

"Icemokka" <ic******@gmail.comschreef in bericht
news:9b**********************************@n20g2000 hsh.googlegroups.com...
Hi,

I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL
2005.

My code looks like this :

fs = New FileStream(sFilePath, FileMode.Open)
Dim ByteArray(fs.Length) As Byte
fs.Read(ByteArray, 0, fs.Length)
fs.Close()

The problem is when I dim the bytearray with my 600Mb file, the
bytearray becomes invalid.
I think the bytearray can only accept an integer as dimension.

Since MSSQL's limit is 2Gb, how should I then write a big file to
MSSQL.
Is there another method to get the whole bytearray of a file to put
into MSSQL?

Regards,
Sven Peeters

Nov 18 '07 #4
Hi,

Thank your for your reply, here is my function. I've turned on Option
Strict and made the changement you proposed.
And suddenly all works perfect. Thank you very much ...

On 18 nov, 11:53, "Armin Zingler" <az.nos...@freenet.dewrote:
"Icemokka" <icemo...@gmail.comschrieb
Hi,
I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL
2005.
My code looks like this :
fs = New FileStream(sFilePath, FileMode.Open)
Dim ByteArray(fs.Length) As Byte
fs.Read(ByteArray, 0, fs.Length)
fs.Close()

As this example shows, it is strongly recommended to switch Option Strict
On. By disabling it, you are not pointed to the fact that a file can be
larger than 2GB. Check the file size before, and if it's not >2GB, you can
safely convert the Long value returend by fs.Length to an Integer.
The problem is when I dim the bytearray with my 600Mb file, the
bytearray becomes invalid.
I think the bytearray can only accept an integer as dimension.

600MB is within the range of an integer, so this is not the problem.
Integer.MaxValue is 2GB.

Be aware that the dim/redim statements expect the upper bound of the array,
so replace "fs.length" by "Cint(fs.length) - 1"
Since MSSQL's limit is 2Gb, how should I then write a big file to
MSSQL.
Is there another method to get the whole bytearray of a file to put
into MSSQL?

Is your current problem getting the byte array into the database or reading
the file into the byte array? If it's the former, have a look at the ADO.Net
group because it is not a VB.Net language related problem. I'm also not sure
whether your problem is to store a 600+ MB file (up to 2GB) into the
database or if you also want to store 2GB+ there. Storing a link to the file
was the only solution (AFAIK).

Armin
Nov 19 '07 #5
Your proposal ( option strict & Cint(lenght - 1 ) fixed my problem.
Now I have a second problem, there are 4 * 600Mb files ready to be put
in the database.
After the first file, I see that my app takes 600Mb of memory ( logic
offcourse ).
But I get an out of memory exception on the second file because the
bytearray has not yet released it's memory ( set it to nothing within
the function ).
How can I force that the garbage collector cleans up ( array's don't
have dispose or finally method ).

On 18 nov, 11:53, "Armin Zingler" <az.nos...@freenet.dewrote:
"Icemokka" <icemo...@gmail.comschrieb
Hi,
I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL
2005.
My code looks like this :
fs = New FileStream(sFilePath, FileMode.Open)
Dim ByteArray(fs.Length) As Byte
fs.Read(ByteArray, 0, fs.Length)
fs.Close()

As this example shows, it is strongly recommended to switch Option Strict
On. By disabling it, you are not pointed to the fact that a file can be
larger than 2GB. Check the file size before, and if it's not >2GB, you can
safely convert the Long value returend by fs.Length to an Integer.
The problem is when I dim the bytearray with my 600Mb file, the
bytearray becomes invalid.
I think the bytearray can only accept an integer as dimension.

600MB is within the range of an integer, so this is not the problem.
Integer.MaxValue is 2GB.

Be aware that the dim/redim statements expect the upper bound of the array,
so replace "fs.length" by "Cint(fs.length) - 1"
Since MSSQL's limit is 2Gb, how should I then write a big file to
MSSQL.
Is there another method to get the whole bytearray of a file to put
into MSSQL?

Is your current problem getting the byte array into the database or reading
the file into the byte array? If it's the former, have a look at the ADO.Net
group because it is not a VB.Net language related problem. I'm also not sure
whether your problem is to store a 600+ MB file (up to 2GB) into the
database or if you also want to store 2GB+ there. Storing a link to the file
was the only solution (AFAIK).

Armin
Nov 19 '07 #6
"Icemokka" <ic******@gmail.comschrieb
Your proposal ( option strict & Cint(lenght - 1 ) fixed my problem.
Now I have a second problem, there are 4 * 600Mb files ready to be
put in the database.
After the first file, I see that my app takes 600Mb of memory (
logic offcourse ).
But I get an out of memory exception on the second file because the
bytearray has not yet released it's memory ( set it to nothing
within the function ).
That's a good question. I'm afraid, I can't answer this. I would have
thought that GC will do it automatically. Do I understand it correctly that
you did set the reference to the array to Nothing /before/ creating the new
array?

I mean,

this
var = nothing
redim var(...)

is not the same as

redim var(...)

because in the 2nd case, first the new array is created before the last
reference to the old array has been cleared. So, the 1st version should be
preferred. Though, I don't know if it helps and if the next Redim will wait
until the GC will have destroyed the previous array.
How can I force that the garbage collector cleans up ( array's don't
have dispose or finally method ).
There's the GC.Collect method, but usually it shouldn't be called manually.
Maybe this is an exception. Let's wait for other answers.
Armin

Nov 19 '07 #7
Can't you just find the maximum size file, allocate the array to that size,
and reuse the array in a loop over all files?

"Armin Zingler" wrote:
"Icemokka" <ic******@gmail.comschrieb
Your proposal ( option strict & Cint(lenght - 1 ) fixed my problem.
Now I have a second problem, there are 4 * 600Mb files ready to be
put in the database.
After the first file, I see that my app takes 600Mb of memory (
logic offcourse ).
But I get an out of memory exception on the second file because the
bytearray has not yet released it's memory ( set it to nothing
within the function ).

That's a good question. I'm afraid, I can't answer this. I would have
thought that GC will do it automatically. Do I understand it correctly that
you did set the reference to the array to Nothing /before/ creating the new
array?

I mean,

this
var = nothing
redim var(...)

is not the same as

redim var(...)

because in the 2nd case, first the new array is created before the last
reference to the old array has been cleared. So, the 1st version should be
preferred. Though, I don't know if it helps and if the next Redim will wait
until the GC will have destroyed the previous array.
How can I force that the garbage collector cleans up ( array's don't
have dispose or finally method ).

There's the GC.Collect method, but usually it shouldn't be called manually.
Maybe this is an exception. Let's wait for other answers.
Armin

Nov 19 '07 #8
That's an idea offcourse, but not very optimal.

On 19 nov, 13:54, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
Can't you just find the maximum size file, allocate the array to that size,
and reuse the array in a loop over all files?

"Armin Zingler" wrote:
"Icemokka" <icemo...@gmail.comschrieb
Your proposal ( option strict & Cint(lenght - 1 ) fixed my problem.
Now I have a second problem, there are 4 * 600Mb files ready to be
put in the database.
After the first file, I see that my app takes 600Mb of memory (
logic offcourse ).
But I get an out of memory exception on the second file because the
bytearray has not yet released it's memory ( set it to nothing
within the function ).
That's a good question. I'm afraid, I can't answer this. I would have
thought that GC will do it automatically. Do I understand it correctly that
you did set the reference to the array to Nothing /before/ creating the new
array?
I mean,
this
var = nothing
redim var(...)
is not the same as
redim var(...)
because in the 2nd case, first the new array is created before the last
reference to the old array has been cleared. So, the 1st version should be
preferred. Though, I don't know if it helps and if the next Redim will wait
until the GC will have destroyed the previous array.
How can I force that the garbage collector cleans up ( array's don't
have dispose or finally method ).
There's the GC.Collect method, but usually it shouldn't be called manually.
Maybe this is an exception. Let's wait for other answers.
Armin- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -
Nov 20 '07 #9
Even better, the array is a locally defined variable of the function
that only handles 1 file.
So after every file dump on SQL, I set the array to nothing and then
it gets out of scope.
But the GC does not recycle the memory fast enough!

On 19 nov, 13:25, "Armin Zingler" <az.nos...@freenet.dewrote:
"Icemokka" <icemo...@gmail.comschrieb
Your proposal ( option strict & Cint(lenght - 1 ) fixed my problem.
Now I have a second problem, there are 4 * 600Mb files ready to be
put in the database.
After the first file, I see that my app takes 600Mb of memory (
logic offcourse ).
But I get an out of memory exception on the second file because the
bytearray has not yet released it's memory ( set it to nothing
within the function ).

That's a good question. I'm afraid, I can't answer this. I would have
thought that GC will do it automatically. Do I understand it correctly that
you did set the reference to the array to Nothing /before/ creating the new
array?

I mean,

this
var = nothing
redim var(...)

is not the same as

redim var(...)

because in the 2nd case, first the new array is created before the last
reference to the old array has been cleared. So, the 1st version should be
preferred. Though, I don't know if it helps and if the next Redim will wait
until the GC will have destroyed the previous array.
How can I force that the garbage collector cleans up ( array's don't
have dispose or finally method ).

There's the GC.Collect method, but usually it shouldn't be called manually.
Maybe this is an exception. Let's wait for other answers.

Armin
Nov 20 '07 #10
What part is less than optimal? At some point you need an array of n-bytes,
and this method just reuses it. When you are done with the loop, then you
release the memory.

"Icemokka" wrote:
That's an idea offcourse, but not very optimal.

On 19 nov, 13:54, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
Can't you just find the maximum size file, allocate the array to that size,
and reuse the array in a loop over all files?

"Armin Zingler" wrote:
"Icemokka" <icemo...@gmail.comschrieb
Your proposal ( option strict & Cint(lenght - 1 ) fixed my problem.
Now I have a second problem, there are 4 * 600Mb files ready to be
put in the database.
After the first file, I see that my app takes 600Mb of memory (
logic offcourse ).
But I get an out of memory exception on the second file because the
bytearray has not yet released it's memory ( set it to nothing
within the function ).
That's a good question. I'm afraid, I can't answer this. I would have
thought that GC will do it automatically. Do I understand it correctly that
you did set the reference to the array to Nothing /before/ creating the new
array?
I mean,
this
var = nothing
redim var(...)
is not the same as
redim var(...)
because in the 2nd case, first the new array is created before the last
reference to the old array has been cleared. So, the 1st version should be
preferred. Though, I don't know if it helps and if the next Redim will wait
until the GC will have destroyed the previous array.
How can I force that the garbage collector cleans up ( array's don't
have dispose or finally method ).
There's the GC.Collect method, but usually it shouldn't be called manually.
Maybe this is an exception. Let's wait for other answers.
Armin- Tekst uit oorspronkelijk bericht niet weergeven -
- Tekst uit oorspronkelijk bericht weergeven -

Nov 20 '07 #11
Gonna pich this solution indeed ...

On 20 nov, 17:45, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
What part is less than optimal? At some point you need an arrayofn-bytes,
and this method just reuses it. When you are done with the loop, then you
release thememory.

"Icemokka" wrote:
That's an idea offcourse, but not very optimal.
On 19 nov, 13:54, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
Can't you just find the maximum size file, allocate the array to that size,
and reuse the array in a loop over all files?
"Armin Zingler" wrote:
"Icemokka" <icemo...@gmail.comschrieb
Your proposal ( option strict & Cint(lenght - 1 ) fixed my problem.
Now I have a second problem, there are 4 * 600Mb files ready to be
put in the database.
After the first file, I see that my app takes 600Mbofmemory(
logic offcourse ).
But I get anoutofmemoryexceptionon the second file because the
bytearray has not yet released it'smemory( set it to nothing
within the function ).
That's a good question. I'm afraid, I can't answer this. I would have
thought that GC will do it automatically. Do I understand it correctly that
you did set the reference to the array to Nothing /before/ creating the new
array?
I mean,
this
var = nothing
redim var(...)
is not the same as
redim var(...)
because in the 2nd case, first the new array is created before the last
reference to the old array has been cleared. So, the 1st version should be
preferred. Though, I don't know if it helps and if the next Redim will wait
until the GC will have destroyed the previous array.
How can I force that the garbage collector cleans up ( array's don't
have dispose or finally method ).
There's the GC.Collect method, but usually it shouldn't be called manually.
Maybe this is anexception. Let's wait for other answers.
Armin- Tekst uit oorspronkelijk bericht niet weergeven -
- Tekst uit oorspronkelijk bericht weergeven -- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -
Nov 21 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim | last post by:
I meet problem on uploading a file with a large file size. Below is my code, how can I modify in order to be able to upload a large file successfully ? Thank for your help. upload.html: <html>...
2
by: plank | last post by:
Hey Peeps, Ok here is my situation.. I have a Java applet which allows the user to select files and upload them to the server. The applet converts the file to Base64 and then POSTS the data to an...
3
by: Amy L. | last post by:
Is there a Buffer size that is optimial based on the framework or OS that is optimal when working with chunks of data? Also, is there a point where the buffer size might not be optimal (too...
6
by: Thomas Due | last post by:
Hi, I am writing an ASP.NET project where I allow users to upload files to the server. I have changed to web.config to allow a total file size of 100MB. My problem is that if the total file size...
1
by: Ron Vecchi | last post by:
When posting a file upload I have taken in consideration the maxRequestLength and set it accordingly. In my case where a posibility of a 30 meg file can be uploaded I set it to 30720. Although...
1
by: Daniel von Fersen | last post by:
When I want to Read the Bytes 1000-2000 from a Stream into a ByteArray using Stream.Read(byteArray,1000,2000) they are written to the positions 1000-2000 in the byteArray. but my Array is...
1
by: Nawaz Ijaz | last post by:
Hi All, Just need to know that how can it be possible to upload large file in chunks of bytes. Suppose i have a large file (100 MB) and i want to upload it in chunks (say 20 KB). How can this be...
5
by: th3dude | last post by:
I've searched quite a bit for this one and i can't find any solid solution for what i'm doing here. Right now i'm geting an xml string from an API, creating an xml file, then read the file with...
4
by: rsgalloway | last post by:
I'm trying to save an image from a Flash AS3 to my server as a jpg file. I found some PHP code to do this, but I want to do this in Python. I'm not quite sure how to convert the following code to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.