转换byte[]中的大文件到SQL Server (c#)中

本文关键字:Server SQL byte 转换 文件 | 更新日期: 2023-09-27 18:13:01

我有一个大小约为1.6 GB的文件。我知道在SQL Server中存储大文件。但是我需要为这个大文件生成blob。

我使用以下代码为大文件生成byte[]:

string filePath = Server.MapPath(filename);
string filename = Path.GetFileName(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();

It is throw

类型为'System '的异常。发生OutOfMemoryException'Mscorlib.dll,但未在用户代码中处理

我想知道如何将这个大文件转换为byte[]

转换byte[]中的大文件到SQL Server (c#)中

你不应该这样做,要么使用块读取文件,或者如果你想将其上传到SharePoint,只需将两个流连接在一起,SharePoint库将完成其余的工作,例如:

FileStream fileStream = File.OpenRead(filename);
SPFile spfile = theLibrary.Files.Add(fileName, fileStream, true);

这是在SharePoint服务器端对象模型中完成的,同样可以在CSOM中完成

     Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fileStream, true);

基本上不能,不能用字节数组。

对于大于2GB的对象可以这样做:

1)在64位系统上以64位模式运行。32位应用程序无法寻址大于1.5GB的内存。

2)正在运行。net Framework V4.5或更高版本。和

3)在app.config中设置gcAllowVeryLargeObjects元素

但是…数组索引器仍然限制为整数-所以你不能对字节数组这样做,因为你的数组索引太大了。

您可以使用流(允许~8TB),但不能使用数组。