dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlLargeObject Class
Members Example

PgSqlLargeObject Class
Represents a PostgreSQL large object.
Syntax
'Declaration
 
Public Class PgSqlLargeObject 
   Inherits System.IO.Stream
   Implements System.Data.SqlTypes.INullableSystem.IDisposable 
 
Remarks

The PgSqlLargeObject class is used to work with PostgreSQL large objects. An instance of PgSqlLargeObject holds an OID of large object and can be used for operations with this large object.

Note: all operations with large objects require an active transaction.

Example
The following example shows how to create and manage large objects with PgSqlLargeObject.
public static void Main(string[] args) {

        PgSqlConnection connection = new PgSqlConnection("user id=postgres;password=postgres;port=5432;host=localhost;");
        connection.Open();

        int oid = CreateLargeObject(connection);
        WriteToLargeObject(connection, oid);
        ReadFromLargeObject(connection, oid);

        Console.ReadLine();
}

public static int CreateLargeObject(PgSqlConnection connection) {

        PgSqlLargeObject lo = new PgSqlLargeObject(connection);
        // Create a new large object in the database.
        lo.Create();
        Console.WriteLine("Created new large object with OID = {0}", lo.Oid);

        return lo.Oid;
}

public static void WriteToLargeObject(PgSqlConnection connection, int oid) {
        // All operations with large objects require an active transaction.
        connection.BeginTransaction();
        string str = "Hello, world!";
        PgSqlLargeObject lo = new PgSqlLargeObject(connection, oid);
        lo.Open();

        if (lo.CanWrite) {
                byte[] buffer = Encoding.UTF8.GetBytes(str);
                lo.Write(buffer, 0, buffer.Length);
        }
        lo.Close();

        Console.WriteLine("Written to the large object : {0}", str);
        connection.Commit();
}

public static void ReadFromLargeObject(PgSqlConnection connection, int oid) {
        // All operations with large objects require an active transaction.
        connection.BeginTransaction();
        string str = string.Empty;
        PgSqlLargeObject lo = new PgSqlLargeObject(connection, oid);
        lo.Open();

        if (lo.CanWrite) {
                byte[] buffer = new byte[lo.Length];
                lo.Read(buffer, 0, buffer.Length);
                str = Encoding.UTF8.GetString(buffer);
        }
        lo.Close();

        Console.WriteLine("Read from the large object : {0}", str);
        connection.Commit();
}
Public Sub Main()
        Dim connection As New PgSqlConnection("user id=postgres;password=postgres;port=5432;host=localhost;")
        connection.Open()

        Dim oid As Integer = CreateLargeObject(connection)
        WriteToLargeObject(connection, oid)
        ReadFromLargeObject(connection, oid)
        Console.ReadLine()
End Sub

Public Function CreateLargeObject(ByVal connection As PgSqlConnection) As Integer
        Dim lo As New PgSqlLargeObject(connection)
        ' Create a new large object in the database.
        lo.Create()
        Console.WriteLine("Created new large object with OID = {0}", lo.Oid)
        Return lo.Oid
End Function

Public Sub WriteToLargeObject(ByVal connection As PgSqlConnection, ByVal oid As Integer)
        ' All operations with large objects require an active transaction.
        connection.BeginTransaction()
        Dim str As String = "Hello, world!"
        Dim lo As New PgSqlLargeObject(connection, oid)
        lo.Open()
        If lo.CanWrite Then
                Dim buffer As Byte() = Encoding.UTF8.GetBytes(str)
                lo.Write(buffer, 0, buffer.Length)
        End If
        lo.Close()
        Console.WriteLine("Written to the large object : {0}", str)
        connection.Commit()
End Sub

Public Sub ReadFromLargeObject(ByVal connection As PgSqlConnection, ByVal oid As Integer)
        ' All operations with large objects require an active transaction.
        connection.BeginTransaction()
        Dim str As String = String.Empty
        Dim lo As New PgSqlLargeObject(connection, oid)
        lo.Open()
        If lo.CanWrite Then
                Dim buffer As Byte() = New Byte(lo.Length - 1) {}
                lo.Read(buffer, 0, buffer.Length)
                str = Encoding.UTF8.GetString(buffer)
        End If
        lo.Close()
        Console.WriteLine("Read from the large object : {0}", str)
        connection.Commit()
End Sub
Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.IO.Stream
         Devart.Data.PostgreSql.PgSqlLargeObject

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also