Click here to Skip to main content
15,896,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am creating a file uploader and I have a question to ask regarding performance. I am using the Entity framework to access the database.

In my database I have a table called Files. The most important field here is the BinaryData field, which is varbinary max. I send parts of the file to the server and then append it to the binary data. This is currently working, but as some users may upload files of any size they want, what will be the most efficient way to append the data?

My current code is as follows:
C#
Append(int fileID, byte[] bytesToAdd)
{
    using (MyDataContext context = new MyDataContext())
    {
         File file = context.Files.First(f=>f.FileID = fileID);
         byte[] newData = new byte[file.BinaryData.Lenght + bytesToAdd.Length];
         file.BinaryData.CopyTo(newData, 0);
         bytesToAdd.CopyTo(newData, file.BinaryData.Length);
         file.BinaryData = newData;
         context.SaveChanges()
    }
}


Is there a more efficient way of doing this? Thanks in advance.
Posted
Updated 27-Mar-12 5:23am
v2

1 solution

A more efficient way would be to send only new data and let SQL server append it.
I don't think there is a way to do this with LINQ.
But in LINQ2SQL you can use a little trick and do something like:

SQL
Append(int fileID, byte[] bytesToAdd)
{
    using (MyDataContext context = new MyDataContext())
    {
         context.ExecuteCommand("UPDATE [Files] SET [BinaryData] = [BinaryData] + {0} WHERE [Id] = {1}", bytesToAdd, fileID);
    }
}


With Entity framework context it's:
SQL
Append(int fileID, byte[] bytesToAdd)
{
    using (MyDataContext context = new MyDataContext())
    {
         context.ExecuteStoreCommand("UPDATE [Files] SET [BinaryData] = [BinaryData] + {0} WHERE [Id] = {1}", bytesToAdd, fileID);
    }
}



Also, if it's binary data, I would use varbinary(MAX)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900