簡體   English   中英

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

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

我有一個數據庫,在二進制列中有一堆保存的文件。 我想使用linq檢索它們。

我有以下代碼,當數據庫中的文件是.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);

當數據庫中的文件是另一種格式(pdf,doc,jpg)時,它損壞了。

將文件寫入數據庫的應用程序是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

評論:

'用於避免UNICODE字符串

使我相信這可能是編碼問題,但是我無法弄清楚它可能在使用哪種編碼。

我也嘗試過使用filestream而不是file.WriteAllBytes,結果得到一個相同的文件:

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

事實證明,在VB6 UI層中有一些意外代碼正在將其他類型與zip文件相互轉換。

我輸入了一些代碼來檢測何時使用了這些文件類型,並將文件重命名為:

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