简体   繁体   中英

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.

I have the following code, which works fine when the file in the database is a .tif file:

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.

The application that wrote the file to the database is VB6, and is working:

   ' 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

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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