简体   繁体   English

sql_variant 的大小限制超过

[英]size limit for the sql_variant exceeds

I am trying to save image from fileupload control into the database我正在尝试将文件上传控件中的图像保存到数据库中

public Byte[] bytes;
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes((Int32)fs.Length);
SqlDataSource2.Update();

protected void SqlDataSource2_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
   e.Command.Parameters["@project_file"].Value = bytes;
}

My database project_file field is set to varbinary(MAX) ,我的数据库project_file字段设置为varbinary(MAX)

but it is throwing an error但它抛出了一个错误

Parameter '@project_file' exceeds the size limit for the sql_variant datatype.参数“@project_file”超出了 sql_variant 数据类型的大小限制。

Please suggest some solution请提出一些解决方案

This is a quote from MSDN on binary and varbinary :这是MSDN 关于 binary 和 varbinary的引述:

Variable-length binary data.可变长度二进制数据。 n can be a value from 1 through 8,000. n 可以是 1 到 8,000 之间的值。 max indicates that the maximum storage size is 2^31-1 bytes. max 表示最大存储大小为 2^31-1 字节。 The storage size is the actual length of the data entered + 2 bytes.存储大小是输入数据的实际长度 + 2 个字节。 The data that is entered can be 0 bytes in length.输入的数据长度可以是 0 字节。 The ANSI SQL synonym for varbinary is binary varying. varbinary 的 ANSI SQL 同义词是二进制变化的。

varbinary(MAX) can hold an image that is ~2GB of size. varbinary(MAX)可以保存大小约为 2GB 的图像。

Solution to your problem:解决您的问题:

You forgot to specify the type in your code.您忘记在代码中指定类型。 You need to set the correct SqlDbType .您需要设置正确的SqlDbType

e.Command.Parameters["@project_file"].SqlDbType = SqlDbType.VarBinary

What you should also do i set the correct Size .你还应该做什么,我设置了正确的Size

The problem is that sql server assign the type of the parameter as "SQL_Variant".问题是 sql 服务器将参数的类型分配为“SQL_Variant”。

Try to assing the DbType:尝试评估 DbType:

e.Command.Parameters["@project_file"].SqlDbType = SqlDbType.Image
e.Command.Parameters["@project_file"].Value = bytes;

Do not use SQL_VARIANT as your datatype in your C# program.不要在 C# 程序中使用 SQL_VARIANT 作为数据类型。 Use the following type instead:请改用以下类型:

Dim binaryStream As SqlBinary

See http://msdn.microsoft.com/en-us/library/a1904w6t(VS.80).aspx请参阅http://msdn.microsoft.com/en-us/library/a1904w6t(VS.80).aspx

PS when you use datatypes of Image and Blob you kill a kitten. PS 当您使用 Image 和 Blob 数据类型时,您会杀死一只小猫。 These datatypes are deprecated and store no more then a varbinary(MAX).这些数据类型已被弃用,并且仅存储 varbinary(MAX)。

Side note on the use of Image vs Varbinary(max):关于使用 Image vs Varbinary(max) 的旁注:

image Variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes. image 从 0 到 2^31-1 (2,147,483,647) 字节的可变长度二进制数据。

varbinary [ ( n | max) ] (maximum storage size is 2^31-1 bytes.) Variable-length binary data. varbinary [ ( n | max) ](最大存储大小为 2^31-1 字节。)可变长度二进制数据。 n can be a value from 1 through 8,000. n 可以是 1 到 8,000 之间的值。 max indicates that the maximum storage size is 2^31-1 bytes. max 表示最大存储大小为 2^31-1 字节。 The storage size is the actual length of the data entered + 2 bytes.存储大小是输入数据的实际长度 + 2 个字节。 The data that is entered can be 0 bytes in length.输入的数据长度可以是 0 字节。 The ANSI SQL synonym for varbinary is binary varying. varbinary 的 ANSI SQL 同义词是二进制变化的。

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

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