2011-01-22 12 views
2

から取り出すとき、私はStackOverflowの上で次のコードが見つかりました:文字列DOCXとXLSX文書の破損SQL

として(文字列として、_ ByValのformNameフォームとして文字列としてByVal docFileDescription、_ ByValのgenericID)

公開機能retreiveDocを

Dim docPathandName As String = String.Empty 

    Using connection As SqlConnection = New SqlConnection(My.Settings.connSQL) 

     Dim command As SqlCommand = New SqlCommand(_ 
      "SELECT fileStore_ID, fileType, imageData FROM FileStore WHERE fileDescription = " & _ 
      "'" & docFileDescription & "'" & _ 
      " AND generic_ID = " & "'" & genericID & "'" & _ 
      " AND appFormName = " & "'" & formName & "'", connection) 

     Dim docType As String = String.Empty 

     '! Writes the BLOB to a file 
     Dim stream As FileStream 

     '! Streams the binary data to the FileStream object. 
     Dim writer As BinaryWriter 

     '! The size of the BLOB buffer. 
     'Dim bufferSize As Integer = 100 
     Dim bufferSize As Integer = 4096 

     '! The BLOB byte() buffer to be filled by GetBytes. 
     Dim outByte(bufferSize - 1) As Byte 

     '! The bytes returned from GetBytes. 
     Dim retval As Long 

     '! The starting position in the BLOB output. 
     Dim startIndex As Long = 0 

     '! The publisher id to use in the file name. 
     Dim fileStore_ID As Int32 

     '! Open the connection and read data into the DataReader. 
     connection.Open() 
     Dim reader As SqlDataReader = command.ExecuteReader(CommandBehavior.SequentialAccess) 

     Do While reader.Read() 
      fileStore_ID = reader.GetInt32(0) 
      docType = reader.GetString(1) 

      '! Create a file to hold the output. 
      stream = New FileStream(_ 
       defaultDrive & "\imageData" & fileStore_ID & docType, FileMode.OpenOrCreate, FileAccess.Write) 

      writer = New BinaryWriter(stream) 

      '! Reset the starting byte for a new BLOB. 
      startIndex = 0 

      '! Read bytes into outByte() and retain the number of bytes returned. 
      retval = reader.GetBytes(2, startIndex, outByte, 0, bufferSize) 

      '! Continue while there are bytes beyond the size of the buffer. 
      Do While retval > 0 
       writer.Write(outByte) 
       writer.Flush() 

       ' Reposition start index to end of the last buffer and fill buffer. 
       startIndex += retval 
       retval = reader.GetBytes(2, startIndex, outByte, 0, bufferSize) 

      Loop 

      writer.Flush() 
      writer.Close() 
      stream.Close() 
     Loop 

     reader.Close() 
     connection.Close() 

     Return (defaultDrive & "imageData" & fileStore_ID & docType) 

    End Using 

End Function 

これは、DOCXおよびXLSXドキュメント以外のすべてに有効です。私は元のC#をVBに変換し、多分何かを逃した。

答えて

1

バッファByte()配列の過剰割り当てについても書き込み部分をチェックしてください。別の例として、私の答えはthis posthereです。

関連する問題