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

时间:2011-05-04 05:47:32

标签: vb.net odbc blob filemaker

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

然而,当使用VB.NET检索它们时,如果文件大小约为> 5MB,我得到以下“无法捕获”的错误(是的,没错,我不能“尝试抓住结束尝试”,它只是崩溃):

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上托管数据库。

对此的任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

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

这一行:

myReader = cmd.ExecuteReader()

应该是:

myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)

这似乎导致了我遇到的问题,其中一些文件会正确打开,而其他文件则不会。希望这有助于某人!添