Andy
Andy

Reputation: 187

Uploading files to SQL Server without using too much memory

I am working with SQL Server 2008 and FileStream data types to save large files in the database.

So far everything is working fine but the problem is my upload method looks like this:

public static void UploadFileToDatabase(string location)
{
   FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read);

   byte[] data = new byte[fs.Length];

   fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
   fs.Close();

   SaveToDatabaseMethod(data)

   data = null;
}

Obviously I am saving the files in memory and then uploading them to server which I think is a really bad practice so is there anyway I can at least limit the amount of memory needed for this ?

What is the best practice in my case ?

Upvotes: 0

Views: 937

Answers (1)

paparazzo
paparazzo

Reputation: 45106

Filestream has a WriteByte method

WriteByte

See The Insert Data Example

Not sure this is best practice as it keeps memory down but adds steps. Would need to test it out. If the entire files does not load need logic to back it out.

Upvotes: 1

Related Questions