简体   繁体   English

插入大于8k字节的字节数组

[英]Inserting a byte array larger than 8k bytes

I'm using the code 我正在使用代码

cmd.Parameters.Add("@Array", SqlDbType.VarBinary).Value = Array;

The SqlDbType.VarBinary description states that it can only handle array's upto 8 K bytes. SqlDbType.VarBinary描述指出它只能处理数组的最大8 K字节。 I have a byte array that represents an image and can go upto 10k bytes. 我有一个代表图像的字节数组,最多可以增加10k字节。

How do I store that in a varbinary(max) column using C#? 如何使用C#将其存储在varbinary(max)列中?

I have no trouble creating the array. 我毫不费力地创建数组。 I'm stuck at this 8k limit when trying to execute the query. 尝试执行查询时,我被限制在8k的限制内。

Edit: Let me clarify, on my machine even pictures upto 15k bytes get stored on the database in the varbinary(MAX) column when I run the asp.net application locally but once I deployed it the pictures would not get stored. 编辑:让我澄清一下,当我在本地运行asp.net应用程序时,即使是高达15k字节的图片也被存储在varbinary(MAX)列中的数据库中,但是一旦部署它,图片就不会被存储。 I then resorted to drastically resizing the images to ensure their size was less that 8K and now the images get stored without any problem. 然后,我采取了大幅调整图像大小的方法,以确保它们的大小小于8K,现在可以毫无问题地存储图像。

Perhaps you could look at the Sql Server FILESTREAM feature since its meant for storing files. 也许您可以看看Sql Server FILESTREAM功能,因为它旨在存储文件。 It basically stores a pointer to your file and the file is stored directly in the filesystem (in the databases data directory). 它基本上存储指向您文件的指针,并且该文件直接存储在文件系统中(在数据库数据目录中)。

I like FILESTREAM since you it means you continue to use the interface to the database (SQLClient for example) rather then breaking out to an adhoc method to read/write files to the harddrive. 我喜欢FILESTREAM,因为它意味着您继续使用数据库的接口(例如SQLClient),而不是使用临时方法来将文件读/写到硬盘驱动器。 This means security is managed for you in that your app doesn't need special permissions to access the filesystem. 这意味着可以为您管理安全性,因为您的应用无需特殊权限即可访问文件系统。

Quick google gave this acticle on using filestream in c# but I'm sure there are many others. 快速谷歌给了这篇文章关于在c#中使用文件流的信息,但是我敢肯定还有很多其他的东西。

UPDATE following OP EDIT OP EDIT之后的更新

So once deployed to other server the upload fails? 那么,一旦部署到其他服务器,上传会失败吗? Perhaps the problem is not the sql insert but that there is a http request content length limit imposed - for example in your web.config the httpRuntime element has the maxRequestLength attribute. 也许问题不在于sql插入,而是强加了http请求内容的长度限制-例如,在您的web.config中, httpRuntime元素具有maxRequestLength属性。 If this is set to a low value perhaps this is the problem. 如果将其设置为较低的值,则可能是问题所在。 So you could set to something like this (sets max to 6MB well over the 10kb problem): 因此,您可以将其设置为以下内容(在10kb的问题上,最大设置为6MB):

<system.web>
    <httpRuntime maxRequestLength="6144" />

The only thing here is the limit it 4MB buy default :| 这里唯一的限制是它默认购买4MB的限制:|

No, this is what the description actually says: 不,这就是描述中实际所说的:

Array of type Byte. 字节类型的数组。 A variable-length stream of binary data ranging between 1 and 8,000 bytes. 长度可变的二进制数据流,范围在1到8,000个字节之间。 Implicit conversion fails if the byte array is greater than 8,000 bytes. 如果字节数组大于8,000个字节,则隐式转换将失败。 Explicitly set the object when working with byte arrays larger than 8,000 bytes. 使用大于8,000个字节的字节数组 时,显式设置对象

I would assume that what that actually means is that you cannot use AddWithValue to have a parameter infer the type as VarBinary if the byte array is over 8000 elements. 我认为这实际上意味着如果字节数组超过8000个元素,则不能使用AddWithValue来让参数将类型推断为VarBinary。 You would have to use Add, specify the type of the parameter yourself and then set the Value property, ie use this: 您将必须使用添加,自己指定参数的类型,然后设置Value属性,即使用以下方法:

command.Parameters.Add("@MyColumn", SqlDbType.VarBinary).Value = myByteArray;

rather than this: 而不是这样:

command.Parameters.AddWithValue("@MyColumn", myByteArray);

Adding the length of data seems to be the fix 解决方法是增加数据长度

var dataParam = cmd.Parameters.AddWithValue("@Data", (object)data.Data ?? DBNull.Value);
                if (data.Data != null)
                {
                    dataParam.SqlDbType = SqlDbType.VarBinary;
                    dataParam.Size = data.Data.Length;
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM