dotConnect for Oracle Documentation
OracleLob Class
Members  Example  See Also  support@devart.com
Devart.Data.Oracle Namespace : OracleLob Class

Represents LOB data type stored on an Oracle server.

Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.IO.Stream
         Devart.Data.Oracle.OracleLob
            Devart.Data.Oracle.OracleBFile

Syntax

Visual Basic (Declaration) 
Public Class OracleLob 
   Inherits System.IO.Stream
   Implements INullableICloneableIComparableIDisposable 
C# 
public class OracleLob : System.IO.Stream, INullableICloneableIComparableIDisposable  

Remarks

An OracleLob differs from an OracleBFile in that data is stored on the server instead of in the physical file in the operating system. It also supports writing, unlike an OracleBFile, which is always read-only.

An OracleLob may be one of these OracleDbType data types:

OracleDBType data type Description
OracleDbType.Blob A variable-length stream of binary data.
OracleDbType.BFile A reference to the physical file in the operating system where Oracle is installed.
OracleDbType.Clob A variable-length stream (single-byte and multibyte character data).
OracleDbType.NClob National character data of variable length.

To read data use Read and OracleLob.Seek methods.

Example

In this example two functions are presented. First can upload a file onto a server, and second can retrieve a LOB field and paste it into a file. Notice that two classes used here (BinaryWriter and BinaryReader) to provide more flexible stream manipulation are actually redundant for simple data transfer operations.

The following table is used for the sample:

CREATE TABLE Pictures (
  ID NUMBER(4) NOT NULL PRIMARY KEY,
  PicName VARCHAR2(50),
  Picture BLOB
)
C#csharpCopy Code
public void DownloadBlob(OracleConnection myConnection)
{
  OracleCommand myCommand = new OracleCommand("SELECT * FROM Pictures", myConnection);
  myConnection.Open();
  OracleDataReader myReader = myCommand.ExecuteReader(System.Data.CommandBehavior.Default);
  try
  {
    while (myReader.Read())
    {
      OracleLob myLob = myReader.GetOracleLob(myReader.GetOrdinal("Picture"));
      if (!myLob.IsNull)
      {
        string FN = myReader.GetString(myReader.GetOrdinal("PicName"));
        FileStream fs = new FileStream("D:\\Tmp\\" + FN + ".bmp", FileMode.Create);
        BinaryWriter w = new BinaryWriter(fs);
        w.Write((byte[])myLob.Value);
        w.Close();
        fs.Close();
        Console.WriteLine(FN + " downloaded.");
      }
    }
  }
  finally
  {
    myReader.Close();
    myConnection.Close();
  }
}

public void UploadBlob(OracleConnection myConnection)
{
  FileStream fs = new FileStream("D:\\Tmp\\_Water.bmp", FileMode.Open, FileAccess.Read);
  BinaryReader r = new BinaryReader(fs);
  myConnection.Open();
  OracleLob myLob = new OracleLob(myConnection,OracleDbType.Blob);
  int streamLength = (int)fs.Length;
  myLob.Write(r.ReadBytes(streamLength), 0, streamLength);
  OracleCommand myCommand = new OracleCommand("INSERT INTO Pictures (ID, PicName, Picture) VALUES(1,'Water',:Pictures)", myConnection);
  OracleParameter myParam = myCommand.Parameters.Add("Pictures", OracleDbType.Blob);
  myParam.OracleValue = myLob;
  try
  {
    Console.WriteLine(myCommand.ExecuteNonQuery() + " rows affected.");
  }
  finally
  {
    myConnection.Close();
    r.Close();
    fs.Close();
  }
}
C#Visual BasicCopy Code
Public Sub DownloadBlob(ByVal myConnection As OracleConnection)
  Dim myCommand As New OracleCommand("SELECT * FROM Pictures", myConnection)
  myConnection.Open()
  Dim myReader As OracleDataReader = myCommand.ExecuteReader(System.Data.CommandBehavior.Default)
  Try
    While myReader.Read()
      Dim myLob As OracleLob = myReader.GetOracleLob(myReader.GetOrdinal("Picture"))
      If Not myLob.IsNull Then
        Dim FN As String = myReader.GetString(myReader.GetOrdinal("PicName"))
        Dim fs As FileStream = New FileStream("D:\Tmp\" + FN + ".bmp", FileMode.Create)
        Dim w As BinaryWriter = New BinaryWriter(fs)
        w.Write(myLob.Value)
        w.Close()
        fs.Close()
        Console.WriteLine(String.Concat(FN, " downloaded."))
      End If
    End While
  Finally
    myReader.Close()
    myConnection.Close()
  End Try
End Sub

Public Sub UploadBlob(ByVal myConnection As OracleConnection)
  Dim fs As FileStream = New FileStream("D:\Tmp\_Water.bmp", FileMode.Open, FileAccess.Read)
  Dim r As BinaryReader = New BinaryReader(fs)
  myConnection.Open()
  Dim myLob As OracleLob = New OracleLob(myConnection, OracleDbType.Blob)
  Dim streamLength As Int32 = fs.Length
  myLob.Write(r.ReadBytes(streamLength), 0, streamLength)
  Dim myCommand As OracleCommand = New OracleCommand("INSERT INTO Pictures (ID, PicName, Picture) VALUES(1,'Water',:Pictures)", myConnection)
  Dim myParam As OracleParameter = myCommand.Parameters.Add("Pictures", OracleDbType.Blob)
  myParam.OracleValue = myLob
  Try
    Console.WriteLine(myCommand.ExecuteNonQuery() & " rows affected.")
  Finally
    myConnection.Close()
    r.Close()
    fs.Close()
  End Try
End Sub

Requirements

Namespace: Devart.Data.Oracle

Platforms:Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7, Windows 8, Windows 10, Windows Server 2012 family.

See Also