我正在尝试将文件上传控件中的图像保存到数据库中
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/