附加二进制数据的有效方法 [英] Efficient way to append binary data

查看:105
本文介绍了附加二进制数据的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在创建一个文件上传器,我想问一个有关性能的问题.我正在使用实体框架访问数据库.

在我的数据库中,有一个名为文件"的表.这里最重要的字段是BinaryData字段,它是varbinary max.我将文件的一部分发送到服务器,然后将其附加到二进制数据中.目前这是可行的,但是由于某些用户可以上传所需大小的文件,因此追加数据的最有效方法是什么?

我当前的代码如下:

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:

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.

推荐答案

一种更有效的方法是只发送新数据,然后让SQL Server附加它. 我认为LINQ无法做到这一点.
但是在LINQ2SQL中,您可以使用一些技巧,例如:

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:

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:

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




另外,如果是二进制数据,我会使用varbinary(MAX)




Also, if it''s binary data, I would use varbinary(MAX)


这篇关于附加二进制数据的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆