Inserting a byte array larger than 8k bytes

我是研究僧i 提交于 2019-12-24 12:29:05

问题


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. I have a byte array that represents an image and can go upto 10k bytes.

How do I store that in a varbinary(max) column using C#?

I have no trouble creating the array. I'm stuck at this 8k limit when trying to execute the query.

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. 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.


回答1:


Perhaps you could look at the Sql Server FILESTREAM feature since its meant for storing files. 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. 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.

UPDATE following 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. 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):

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

The only thing here is the limit it 4MB buy default :|




回答2:


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. Implicit conversion fails if the byte array is greater than 8,000 bytes. Explicitly set the object when working with byte arrays larger than 8,000 bytes.

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. You would have to use Add, specify the type of the parameter yourself and then set the Value property, i.e. use this:

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

rather than this:

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



回答3:


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;
                }


来源:https://stackoverflow.com/questions/22111056/inserting-a-byte-array-larger-than-8k-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!