简体   繁体   English

使用C#检索VB6应用程序保存在SQL Binary字段中的文件

[英]Using C# to retrieve a file saved in a SQL Binary field by a VB6 application

I have a database with a bunch of saved files in a binary column. 我有一个数据库,在二进制列中有一堆保存的文件。 I would like to retrieve them using linq. 我想使用linq检索它们。

I have the following code, which works fine when the file in the database is a .tif file: 我有以下代码,当数据库中的文件是.tif文件时,它可以正常工作:

var Bytes = TblBriefingImages.Where(si => si.ImageName == t.FileName 
                                            && si.ImageType == t.ImageType 
                                            && "B" + si.SourceDocumentNumber == t.BriefingNumber)
                                            .Select(si => si.Image.ToArray())
                                            .SingleOrDefault();

            File.WriteAllBytes(t.FullPath,Bytes);

When the file in the database is another format (pdf, doc, jpg) it comes out corrupted. 当数据库中的文件是另一种格式(pdf,doc,jpg)时,它损坏了。

The application that wrote the file to the database is VB6, and is working: 将文件写入数据库的应用程序是VB6,并且正在运行:

   ' Copy the bitmap to the temporary file chunk by chunk:
    Dim buffer() As Byte                    'used to avoid UNICODE string
    intHandle = FreeFile
    Open strTempFileName For Binary Access Write As #intHandle

    For i = 0 To lngBuffers
        buffer() = !Image.GetChunk(BUFFER_SIZE)
        Put #intHandle, , buffer()
    Next i

    Close #intHandle

The comment: 评论:

'used to avoid UNICODE string '用于避免UNICODE字符串

leads me to believe it might be an encoding issue, but I can't work out what encoding it might be using. 使我相信这可能是编码问题,但是我无法弄清楚它可能在使用哪种编码。

I have also tried using filestream instead of file.WriteAllBytes, and results in an identical file: 我也尝试过使用filestream而不是file.WriteAllBytes,结果得到一个相同的文件:

var fs = File.Create(t.FullPath,Bytes.Length);
fs.Write(Bytes,0,Bytes.Length);
fs.Close();

It turns out that there was some code unexpected in the VB6 UI layer that was converting those other types to and from zip files. 事实证明,在VB6 UI层中有一些意外代码正在将其他类型与zip文件相互转换。

I put in some code to detect when those files types were used and renamed the files as such: 我输入了一些代码来检测何时使用了这些文件类型,并将文件重命名为:

var newPath = Path.ChangeExtension(T.FullPath, ".Zip");
File.Move(T.FullPath, newPath);

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

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