使用 VB.NET 从 Filemaker 的 BLOB(容器)字段中读取大文件 (>5MB)



我正在尝试使用FileMaker自己的ODBC驱动程序从FileMaker 11容器字段读取二进制文件。我能够将文件写入数据库,这工作得很好。手动检索它们很好,文件看起来很好,没有损坏。

然而,当使用VB检索它们时。. NET,如果文件大小大约> 5MB,我得到以下"不可捕获"错误(是的,没错,我不能"Try Catch End Try",它只是崩溃):

System.AccessViolationException was unhandled
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

检索大小为<5MB的文件就可以了。

下面是代码和它崩溃的地方:

Using cn2 As New Odbc.OdbcConnection(G_AppSettings.ODBC_FileMaker("xxx", "xxx", "xxx")) ' Establish ODBC connection to FileMaker DB
     cn2.Open()
     cmd = New OdbcCommand("SELECT DocumentName, GetAs(DocumentContainer, 'FILE') FROM Documents WHERE DocumentID = " & id, cn2)
     myReader = cmd.ExecuteReader()
     If myReader.Read() Then
          ' get the name of the file
          If Not myReader.IsDBNull(0) Then
                TempDoc.FileName = myReader.GetValue(0)
          End If

          ' check for problems:
          If TempDoc.FileName = "" Then
                MsgBox("Error: file name not specified. Could not open file.")
                Exit Sub
          End If
          If tempDir = "" Then
                MsgBox("Error: can't find local temp directory. Could not open file.")
                Exit Sub
          End If
          ' -----------------------------
          ' SAVE FILE IN TEMP WINDOWS DIR
          ' -----------------------------
          fs = New FileStream(tempDir & "" & TempDoc.FileName, FileMode.OpenOrCreate, FileAccess.Write)
          bw = New BinaryWriter(fs)
          ' Read bytes into outbyte() and retain the number of bytes returned.
          Dim ds1 = myReader.GetDataTypeName(1)
          Dim ds2 = myReader.GetFieldType(1)
          Dim bytesRead = myReader.GetBytes(1, 0, outbyte, 0, bufferSize) < - CRASHES HERE
          retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
          ' Continue reading and writing while there are bytes beyond the size of the buffer.
          Do While retval = bufferSize
                bw.Write(outbyte)
                bw.Flush()
                ' Reposition the start index to the end of the last buffer and fill the buffer.
                startIndex += bufferSize
                retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
          Loop
          ' Write the remaining buffer.
          bw.Write(outbyte, 0, retval - 1)
          bw.Flush()
          ' Close the output file.
          bw.Close()
          fs.Close()
     End If
     cn2.Close()
End Using

我使用的是Windows XP/7客户端,并在FileMaker Advanced Server 11上托管数据库。

任何帮助在这将是伟大的!

经过反复试验,我终于回答了自己的问题。上面发布的所有代码都可以正常工作,除了我忘记指定OdbcDataReader

的行为这条线

:

myReader = cmd.ExecuteReader()
应:

myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)

似乎这导致了我遇到的问题,有些文件会正确打开,而另一些则不会。希望这对大家有所帮助!蒂姆。

最新更新