And what does GetData ? Are you using option strict ? It would seem you use
a byte at an inappropriate place.
The simplest sample I could come with is :
Dim Connection As New OleDbConnection(ConnectionString)
Dim Command As New OleDbCommand("SELECT TOP 1 Blob FROM Table1",
Connection)
Connection.Open()
Dim Reader As OleDb.OleDbDataReader =
Command.ExecuteReader(CommandBehavior.SequentialAccess)
Reader.Read()
Dim Blob(255) As Byte
Reader.GetBytes(0, 0, Blob, 0, 255)
Reader.Close()
Command.Dispose()
Connection.Close()
For i As Integer = 0 To UBound(Blob)
If Blob(i) <> i Then MsgBox("Doesn't match")
Debug.Print(i & "/" & Blob(i))
Next
The final check just allows to see that data are retrieved correctly (I
stored a small buffer with value n at the nth position).
Finally it's likely better to retrieve regular data using a query and to
handle blob data with another query so that you don't incurs the overhead of
processing potentially huge values when this is not really needed...
---
Patrice
Webbyz said:
The size of the file is a .pdf which is about 750kb.
Error as follows.
"An error was thrown by the ADO.Net component in the GetData function.
Error Details: Syntax error (missing operator) in query expression
"System.Byte[]""
Database is a Access 2000 .mdb With three fields.
1.) ID - AutoNumber
2.) Description - Text
3.) File - OLEObject
Here is my code so far.
Imports System.IO
Public Class Form1
Public Shared Sub AddFile(ByVal pdfFilePath As String)
Dim newFile() As Byte = GetFile(pdfFilePath)
UpdateData("INSERT INTO Table1 (Description, File) VALUES('" &
My.Computer.FileSystem.GetFileInfo(pdfFilePath).Name & "'," &
newFile.ToString & ")")
End Sub
Public Shared Function GetFile(ByVal filePath As String) As Byte()
Dim stream As FileStream = New FileStream( _
filePath, FileMode.Open, FileAccess.Read)
Dim reader As BinaryReader = New BinaryReader(stream)
Dim newFile() As Byte = reader.ReadBytes(stream.Length)
reader.Close()
stream.Close()
Return newFile
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
With OFD
.Title = "Select file..."
.Filter = "All Files (*.*)|*.*|PDF Documents (*.pdf)|
*.pdf"
.ShowHelp = False
.ShowReadOnly = False
.ShowDialog()
.Multiselect = False
If File.Exists(.FileName) Then
AddFile(.FileName)
Else
MsgBox("File not found!")
End If
End With
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Dim myDataset As DataSet
' Grab all records in this table with ID 1 or whatever number
you want
myDataset = GetData("SELECT * FROM [table1] WHERE [ID]=1",
"myFile")
If myDataset.Tables("myFile").Rows.Count > 0 Then ' found the
File
Label1.Text =
myDataset.Tables("myFile").Rows(0).Item("Description").ToString
' Get a temp file form windows
Dim TempFileName As String =
My.Computer.FileSystem.GetTempFileName
' get the file data and put it into a file or something
Label2.Text =
myDataset.Tables("myFile").Rows(0).Item("File").ToString
Else
MsgBox("No record found!")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
End Class