views:

171

answers:

4

Hi,

I am using C# and I am having trouble loading large files into a binary field use byte arrays.

Basically, if I load a file that is too large, I get memory problems.

Is there a way to load a file into a binary field without using large amounts of ram, i.e. avoid loading the file into memory first?

If it helps, I am using Advantage Database Server and this is using a winforms app not a web app.

Regards

A: 

Unless you choose to simply add a reference in the database to where the file is located it must go through memory first, so id advise looking into a solution that loads the file x bytes at a time.

g_g
A: 

I've done this before using "UPDATE .WRITE" I read chunks of the file from a stream and append the data to the database field. Search for .WRITE on this page: http://msdn.microsoft.com/en-us/library/ms177523.aspx.

Unfortunately that is for SQL Server. I think Advantage Database Server will vary a bit here, but maybe this will send you in the right direction.

David Hogue
+2  A: 

You can use the Advantage Client Engine (ACE) directly and call the AdsSetBinary API to set the blob in chunks. Sorry I don't have time right now to write an example for you. You will need to add this prototype to your c# file:

[DllImport("ace32.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Winapi )]
              public static extern uint AdsSetBinary( IntPtr hTable, String pucFldName, ushort usBinaryType, uint ulTotalLength, uint ulOffset, byte[] pucBuf, uint ulLen );   

And here is a link to the C API reference:

http://devzone.advantagedatabase.com/dz/webhelp/Advantage9.1/mergedProjects/ace/api2/adssetbinary.htm

To get the handle to pass the API, see the AdsExtendedReader.AdsHandle documentation.

Update: I just saw Alex's answer and it is better than mine, I didn't know that we already wrapped the AdsSetBinary call with a .NET method. You might want to mark his answer as the correct answer to this question. :)

Jeremy Mullin
+5  A: 

The AdsExtendedReader.SetBytes can be used to load the blob in chunks. It encapsulates the the call to the AdsSetBinary() that Jeremy mentioned.

see AdsExtendedReader.SetBytes.

Alex W