Is "OLE Objects" supported in .NET?

  • Thread starter Thread starter Rashad Rivera
  • Start date Start date
R

Rashad Rivera

I need to figure out how to put files like a .JPG or .DOC into a Access2000
DB via .NET. Does the Odbc library support this functionality? If not, any
other suggestions?

- Rashad Rivera
 
I appreciate the links, but I need to use Access databases only. This is
using SQL.
 
On Mon, 21 Jun 2004 15:19:31 -0400, "Rashad Rivera"

¤ I need to figure out how to put files like a .JPG or .DOC into a Access2000
¤ DB via .NET. Does the Odbc library support this functionality? If not, any
¤ other suggestions?

The following uses an Access OLE Object field:

Sub WriteBlobToAccess()

Dim SourceFilePath As String
SourceFilePath = "e:\My Documents\Greenstone.bmp"
Dim AccessConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\db1.mdb")
Dim AccessCommand As New OleDbCommand("UPDATE Table1 SET OLEField=? WHERE [record id] = 1",
AccessConnection)
Dim FileStreamObject As New System.IO.FileStream(SourceFilePath, IO.FileMode.Open,
IO.FileAccess.Read)
Dim PictureByteArray(CType(FileStreamObject.Length() - 1, Integer)) As Byte
FileStreamObject.Read(PictureByteArray, 0, PictureByteArray.Length)
FileStreamObject.Close()
Dim QueryParameter As New OleDbParameter("@Picture", OleDbType.LongVarBinary,
PictureByteArray.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current,
PictureByteArray)
AccessCommand.Parameters.Add(QueryParameter)
AccessConnection.Open()
AccessCommand.ExecuteNonQuery()
AccessConnection.Close()

End Sub

Sub ReadBlobFromAccess()

Dim DestFilePath As String
Dim RecordId As String
Dim RetVal As Long
Dim FieldLen As Int32

DestFilePath = "e:\My Documents\GreenstoneFromAccess.bmp"
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim AccessConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\db1.mdb")
Dim AccessCommand As New OleDbCommand("SELECT [record ID], OLEField FROM Table1 WHERE
[record id] = 1", AccessConnection)
AccessConnection.Open()
Dim AccessDataReader As OleDbDataReader =
AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
AccessDataReader.Read()
FieldLen = AccessDataReader.Item(1).Length
Dim PictureByteArray(FieldLen - 1) As Byte
Dim startIndex As Integer = 0
RetVal = AccessDataReader.GetBytes(1, startIndex, PictureByteArray, 0,
PictureByteArray.Length)
AccessDataReader.Close()
AccessConnection.Close()
Dim FileStreamObject As New System.IO.FileStream(DestFilePath, IO.FileMode.Create,
IO.FileAccess.Write)
FileStreamObject.Write(PictureByteArray, 0, PictureByteArray.Length)
FileStreamObject.Close()

End Sub


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
As a side note and especially for an Access DB, I would store the files in
the file system and would keep just a link in the DB to prevent working with
a voluminous MDB file...

Patrice

--

Paul Clement said:
On Mon, 21 Jun 2004 15:19:31 -0400, "Rashad Rivera"

¤ I need to figure out how to put files like a .JPG or .DOC into a Access2000
¤ DB via .NET. Does the Odbc library support this functionality? If not, any
¤ other suggestions?

The following uses an Access OLE Object field:

Sub WriteBlobToAccess()

Dim SourceFilePath As String
SourceFilePath = "e:\My Documents\Greenstone.bmp"
Dim AccessConnection As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\db1.mdb")
Dim AccessCommand As New OleDbCommand("UPDATE Table1 SET
OLEField=? WHERE [record id] = 1",
AccessConnection)
Dim FileStreamObject As New System.IO.FileStream(SourceFilePath, IO.FileMode.Open,
IO.FileAccess.Read)
Dim PictureByteArray(CType(FileStreamObject.Length() - 1, Integer)) As Byte
FileStreamObject.Read(PictureByteArray, 0, PictureByteArray.Length)
FileStreamObject.Close()
Dim QueryParameter As New OleDbParameter("@Picture", OleDbType.LongVarBinary,
PictureByteArray.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current,
PictureByteArray)
AccessCommand.Parameters.Add(QueryParameter)
AccessConnection.Open()
AccessCommand.ExecuteNonQuery()
AccessConnection.Close()

End Sub

Sub ReadBlobFromAccess()

Dim DestFilePath As String
Dim RecordId As String
Dim RetVal As Long
Dim FieldLen As Int32

DestFilePath = "e:\My Documents\GreenstoneFromAccess.bmp"
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim AccessConnection As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\db1.mdb")
Dim AccessCommand As New OleDbCommand("SELECT [record ID], OLEField FROM Table1 WHERE
[record id] = 1", AccessConnection)
AccessConnection.Open()
Dim AccessDataReader As OleDbDataReader =
AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
AccessDataReader.Read()
FieldLen = AccessDataReader.Item(1).Length
Dim PictureByteArray(FieldLen - 1) As Byte
Dim startIndex As Integer = 0
RetVal = AccessDataReader.GetBytes(1, startIndex, PictureByteArray, 0,
PictureByteArray.Length)
AccessDataReader.Close()
AccessConnection.Close()
Dim FileStreamObject As New System.IO.FileStream(DestFilePath, IO.FileMode.Create,
IO.FileAccess.Write)
FileStreamObject.Write(PictureByteArray, 0, PictureByteArray.Length)
FileStreamObject.Close()

End Sub


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
¤ As a side note and especially for an Access DB, I would store the files in
¤ the file system and would keep just a link in the DB to prevent working with
¤ a voluminous MDB file...

I would wholeheartedly agree with that statement.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
I think I will try that, I totally did not know about MDBs. Thanks for the
advice

- Rashad Rivera
 
Back
Top