c# - sql_variant 的大小限制超过

标签 c# asp.net sql visual-studio

我正在尝试将文件上传控件中的图像保存到数据库中

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

我的数据库 project_file 字段设置为 varbinary(MAX),

但是报错了

Parameter '@project_file' exceeds the size limit for the sql_variant datatype.

请提出一些解决方案

最佳答案

这是来自 MSDN on binary and varbinary 的引述:

Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length. The ANSI SQL synonym for varbinary is binary varying.

varbinary(MAX) 可以容纳约 2GB 大小的图像。

您的问题的解决方案:

您忘记在代码中指定类型。您需要设置正确的 SqlDbType .

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

我还应该设置正确的 Size .

关于c# - sql_variant 的大小限制超过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6581745/

相关文章:

c# - 将空项目添加到有界组合框

jquery - 无法在 jquery 中访问 asp.net 控件 ID

asp.net 网站手机支持

sql - 夏令时导致数据库中的日期不正确

c# - 使用属性名称的字符串表示在 LINQ C# 中计算平均值

c# - Android Azure 删除所有行

c# - 在 View 中显示字符串?

c# - 插入具有一对多关系的新记录/更新现有记录

sql - 执行存储过程和查询的区别(因类型算术溢出)

Sql 函数 - NANVL - 不同的行为